shell脚本--使用sort、uniq、tr等命令统计文件内容行数,单词个数

本文介绍了如何利用shell脚本,结合sort、uniq和tr等命令,来统计文本文件的行数、相同行的数量以及特定单词和所有单词的出现次数,并提供了两种不同的操作方法。
摘要由CSDN通过智能技术生成

假设有个文件hello.txt

[root@liuzhiwei-centos6 ~]# cat hello.txt 
hello world welcome
hello world
world welcome
hello welcome
hello world
hello world welcome
world world heihei
welcome hello
hello world
world heihei
hello welcome
world welcome

(1)sort、uniq统计文本行数、相同行数量

统计行数:
[root@liuzhiwei-centos6 ~]# cat hello.txt | wc -l
12

统计每个相同行的数量:
[root@liuzhiwei-centos6 ~]# cat hello.txt | sort | uniq -c
      2 hello welcome
      3 hello world
      2 hello world welcome
      1 welcome hello
      1 world heihei
      2 world welcome
      1 world world heihei

根据行数进行排序:
[root@liuzhiwei-centos6 ~]# cat hello.txt | sort | uniq -c | sort -n
      1 welcome hello
      1 world heihei
      1 world world heihei
      2 hello welcome
      2 hello world welcome
      2 world welcome
      3 hello world

(2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值