我正在尝试使用 bash 命令获取大型 txt 文件中各种字符串的数量。
即使用 bash 查找字符串“pig”、“horse”和“cat”的计数,并得到输出“pig: 7, horse: 3, cat: 5”。我想要一种只搜索 txt 文件一次的方法,因为它非常大(所以我不想在整个 txt 文件中搜索“pig”,然后返回搜索“horse”等)
任何有关命令的帮助将不胜感激。谢谢!
最佳答案
grep -Eo 'pig|horse|cat' txt.file | sort | uniq -c | awk '{print $2": "$1}'
将其分解:
grep -Eo 'pig|horse|cat' Print all the occurrences (-o) of the
extended (-e) regex
sort Sort the resulting words
uniq -c Output unique values (of sorted input)
with the count (-c) of each value
awk '{print $2": "$1}' For each line, print the second field (the word)
then a colon and a space, and then the first
field (the count).
关于Bash查找大文件中多个字符串的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145077/