[英]Find the number of occurences of certain string sequences
I want to count the number of occurences of the IP address 192.168.1.10
in a text file using grep | wc
我想使用
grep | wc
文本文件中IP地址192.168.1.10
的出现次数grep | wc
grep | wc
. grep | wc
。
The command I use is: 我使用的命令是:
cat ./capture.txt|grep "192.168.1.10"|wc -w
which returns 0, and I don't know why. 它返回0,我不知道为什么。
Here is the content of my .txt file: 这是我的.txt文件的内容:
give this a try: 试试看:
grep -Fwo '192.168.1.10' file|wc -l
-F
makes the grep take your pattern as literal string instead of regex -F
使grep将您的模式作为原义字符串而不是正则表达式 -w
excludes 192.168.1.101
or 192.168.1.100
-w
排除192.168.1.101
或192.168.1.100
-o
lists each match in a line. -o
在一行中列出每个匹配项。 grep does line based match, if your pattern matched twice in a line, the result of occurrence count may be wrong. cat ./capture.txt | grep "\b192\.168\.1\.10\b" -c
\\.
search for dot, not any character \\b
match at the beginning or end of a word \\b
在单词的开头或结尾匹配 -c
return the number of occurrences -c
返回出现的次数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.