How to use grep in Linux to count string occurrences?
grep -oi "error" /home/xlog/xerrorlogs.txt | wc -l
Sample output:
5 (if there are 5 matches)
Grep will look for the word "error" in xerrorlogs.txt and count its occurrence per line.
Grep will find the string occurrence without case sensitivity.
Grep parameters:
-o
--only-matching
Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line.
-i
--ignore-case
Ignore case distinctions, so that characters that differ only in case match each other.
wc - print newline, word, and byte counts for each file
-l, --lines
print the newline counts
If "wc -l" is omitted:
grep -oi "error" /home/xlog/xerrorlogs.txt
Grep will display the match string.
Sample output:
Error
Error
Error
Error
Error
If Grep uses -c parameter:
grep -oic "error" /home/xlog/xerrorlogs.txt
Sample output:
4
The output is "4" if there are 5 matches since counting starts at zero, need to pipe to wc -l to start counting at "1".
================================
Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet:
https://play.google.com/store/apps/details?id=com.LinuxMobileKit
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html
Divine Mercy Chaplet Guide (A Powerful prayer):
https://play.google.com/store/apps/details?id=com.dmercyapp
Comments
Post a Comment