简体   繁体   English

查找某些字符串序列的出现次数

[英]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.101192.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. grep执行基于行的匹配,如果您的模式在一行中匹配两次,则出现次数的结果可能是错误的。
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM