linux 文件内容频率,Linux-Awk:来自一个文本文件的单词频率,如何输出到myFile.txt?...

给定.txt文件,这些文件之间用空格分隔,例如:

But where is Esope the holly Bastard

But where is

和Awk函数:

cat /pathway/to/your/file.txt | tr ' ' '\n' | sort | uniq -c | awk '{print $2"@"$1}'

我在控制台中得到以下输出:

1 Bastard

1 Esope

1 holly

1 the

2 But

2 is

2 where

如何进入打印到myFile.txt中?

我实际上有300.000行,近200万个单词.最好将结果输出到文件中.

编辑:使用的答案(通过@Sudo_O):

$awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" myfile.txt | sort > myfileout.txt

解决方法:

您的管道效率不是很高,您应该用awk来完成整个工作:

awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" file > myfile

如果要按排序顺序输出:

awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" file | sort > myfile

管道给出的实际输出为:

$tr ' ' '\n' < file | sort | uniq -c | awk '{print $2"@"$1}'

Bastard@1

But@2

Esope@1

holly@1

is@2

the@1

where@2

注意:在这里使用cat是没有用的,我们只能使用

$tr ' ' '\n' < file | sort | uniq -c

1 Bastard

2 But

1 Esope

1 holly

2 is

1 the

2 where

我们可以再次排序以sed删除前导空格:

$tr ' ' '\n' < file | sort | uniq -c | sort | sed 's/^\s*//'

1 Bastard

1 Esope

1 holly

1 the

2 But

2 is

2 where

但是就像我在一开始提到的那样,让awk处理它:

$awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" file | sort

1 Bastard

1 Esope

1 holly

1 the

2 But

2 is

2 where

标签:frequency-analysis,linux,shell,awk,word-frequency

来源: https://codeday.me/bug/20191013/1905844.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值