如何使用 unix 命令仅计算和显示重复多次的单词?
How can I count and display only the words that are repeated more than once using unix commands?
我正在尝试仅统计并显示文件中重复多次的单词。基本思路是:
- 你得到一个文件,其中包含名称和字符,如逗号、冒号、斜线等。
- 使用
cut
命令仅显示文件中的名字(也允许使用其他命令)。
- 统计并只显示重复多次的名字。
我到了计算并显示所有名称的地步。但是,我还没有找到一种方法来显示和计算那些重复不止一次的名字。
这是文件的一部分:
user1:x:80:200:Mia,Spurs:/home/user1:/bin/bash
user2:x:80:200:Martha,Dalton:/home/user2:/bin/bash
user3:x:80:200:Lucy,Carlson:/home/user3:/bin/bash
user4:x:80:200:Carl,Bingo:/home/user4:/bin/bash
这是我能够做到的:
Daniel@Daniel-MacBook-Pro Files % cut -d ":" -f 5-5 file1 | cut -d "," -f 1-1 | sort -n | uniq -c
1 Mia
3 Martha
1 Lucy
1 Carl
1 Jessi
1 Joke
1 Jim
2 Race
1 Sem
1 Shirly
1 Susan
1 Tim
您可以使用 grep
.
过滤掉计数为 1
的行
cut -d ":" -f 5 file1 | cut -d "," -f 1 | sort | uniq -c | grep -v '^ *1 '
我正在尝试仅统计并显示文件中重复多次的单词。基本思路是:
- 你得到一个文件,其中包含名称和字符,如逗号、冒号、斜线等。
- 使用
cut
命令仅显示文件中的名字(也允许使用其他命令)。 - 统计并只显示重复多次的名字。
我到了计算并显示所有名称的地步。但是,我还没有找到一种方法来显示和计算那些重复不止一次的名字。
这是文件的一部分:
user1:x:80:200:Mia,Spurs:/home/user1:/bin/bash
user2:x:80:200:Martha,Dalton:/home/user2:/bin/bash
user3:x:80:200:Lucy,Carlson:/home/user3:/bin/bash
user4:x:80:200:Carl,Bingo:/home/user4:/bin/bash
这是我能够做到的:
Daniel@Daniel-MacBook-Pro Files % cut -d ":" -f 5-5 file1 | cut -d "," -f 1-1 | sort -n | uniq -c
1 Mia
3 Martha
1 Lucy
1 Carl
1 Jessi
1 Joke
1 Jim
2 Race
1 Sem
1 Shirly
1 Susan
1 Tim
您可以使用 grep
.
1
的行
cut -d ":" -f 5 file1 | cut -d "," -f 1 | sort | uniq -c | grep -v '^ *1 '