简体   繁体   English

如何使用grep计数txt文件中五个字母单词的数量?

[英]How can I count the amount of five letter words in a txt file using grep?

我不太擅长linux,并且正在尝试使用grep计数五个字母单词。

Use the c flag to count, look for patterns containing five characters: 使用c标志进行计数,查找包含五个字符的模式:

 $ cat file
 some text file containing many words and sentences.
 $ tr ' ' '\n' < file | grep -c '^[ \t]*[a-zA-Z]\{5\}[ \t]*$'
 1

You can use: 您可以使用:

grep -o -w "\w\{5\}" your_file | wc -w

With -o only matched words will be printed, -w denotes that regex is searched as a word, \\w\\{5\\} - regex string itself (matches 5 continuous word characters). 如果使用-o仅会打印匹配的单词, -w表示将正则表达式作为单词搜索, \\w\\{5\\} -正则表达式字符串本身(匹配5个连续的单词字符)。 So, with your_file containing 因此,与your_file包含

word1 word2 word3
long_word 123 word4

Output of grep -o -w "\\w\\{5\\}" your_file will be grep -o -w "\\w\\{5\\}" your_file将是

word1
word2
word3
word4

Piped wc -w just counts this. 管道wc -w只是计算在内。

Note : If you don't want to match all alphanumeric characters - replace \\w meta-character by something more specific. 注意 :如果您不想匹配所有字母数字字符,请使用更具体的内容替换\\w元字符。 For example [az] - lowercase English letters. 例如[az] -小写英文字母。

This gnu awk (due to mulitple characters in Record Selector) does count how many word have 5 letters. 这个gnu awk (由于Record Selector中的多个字符)确实计算出有5字母的单词。 It does ignore ., etc. 它确实会忽略.,等等。

awk -v RS="[ .,?!]|\n" 'length($0)==5 {a++} END {print a}' file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 grep 命令查找不包含字母“e”的 8 个字母单词的数量? - How can I find the number of 8 letter words that do not contain the letter "e", using the grep command? 如何在 Linux 终端中使用 grep 命令获取四个字母的单词列表? - How can I get a list of four letter words using the grep command in the Linux terminal? 使用 grep 搜索目录中的文件,如何使用 list.txt(或 csv)获取每个单词的出现次数? - Using grep, to search the files in the directories, how can I get the occurrence count of each word using a list.txt(or csv)? 如何使用OR复制文件 - How can I grep a file using OR 如何grep所有带有特定字母的单词? - How to grep all words with a specific letter in it? 如果我通过目录grep recursivley,如何将结果输出保存到简单的.txt文件中? - If I grep recursivley through a directory, how can i save the resulting output to a simple .txt file? 如何将我的grep重定向到另一个目录中的txt文件? - How can I redirect my grep to a txt file, located in another directory? 我正在尝试使用 Grep 命令在 txt 文件中查找命令字符串,然后编写脚本来计算每个文件中的行数 - I am trying to Grep a command to find a string of command in a txt file, then write a script to count the lines in each file 我想 grep 中间有连字符并以大写字母开头的单词+以大写字母开头但不带连字符的单词 - I want to grep words that have a hyphen in the middle and start with uppercase letter + words that start with uppercase letter without hyphen 使用grep -n(数据)file1.txt&gt; file2.txt - using grep -n (data) file1.txt > file2.txt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM