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
.
The command I use is:
cat ./capture.txt|grep "192.168.1.10"|wc -w
which returns 0, and I don't know why.
Here is the content of my .txt file:
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 -w
excludes 192.168.1.101
or 192.168.1.100
-o
lists each match in a line. 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 -c
return the number of occurrences
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.