Re: grep command
by ferreira (Chaplain) on Jan 18, 2007 at 13:29 UTC
|
The first thing you want to check is the grep documentation. This together with a few practice with hashes will easily solve your problem.
Many people here get upset with these questions that read like "Hey, I have a problem. Gimme a solution for free. Oh, I don't mind if you improve on what I didn't tell you." Take a look at how Problem in date checking script was received by comments Re: Problem in date checking script and Re: Problem in date checking script. The reference How (Not) To Ask A Question will be a good reading for you as well. It will bring you more sympathetic and useful answers, while helping you to revert your negative XP score.
A one-liner such as this may count the number of lines the word "hello" was found, just like $ grep hello text.out | wc -l (mentioned by derby at Re: grep command).
$ perl -n -e '$count++ if /\bhello\b/; END { print $count }' text.out
A very simple modification will give you what you want. It has to do with the global modifier in regexes (see perlretut).
Note: I've changed the simple /hello/ to /\bhello\b/ because sanku said he was looking for words. | [reply] [d/l] [select] |
|
|
To get the lines and the count (which is what I think the OP wants):
perl -ne 'if (/\bhello\b/) { print; $count++ } END { print "count: $count\n" }' text.out
And if you want to find "hello" regardless of capitalization:
perl -ne 'if (/\bhello\b/i) { print; $count++ } END { print "count: $count\n" }' text.out
Also, grep from the command line doesn't need wc's help:
grep -c hello text.out
It can match regardless of case too:
grep -ic hello text.out
| [reply] [d/l] [select] |
Re: grep command
by derby (Abbot) on Jan 18, 2007 at 13:15 UTC
|
Well, I was going to give a snarky answer ($ grep hello text.out | wc -l) but then I realized that would only give the number of lines hello appears on. Soooo ... why don't you show us what code you have so far.
| [reply] [d/l] |
|
|
just cheat and make sure the input doesn't have more than one per line :)
tr ' ' '\n' < /tmp/hh | grep -c hello
alternatives:
plain, explicit count (leverage s/// to provide # per line):
perl -lne '$ct += s/hello//g; END {print $ct}' /tmp/hh
Same technique as the tr/grep above, just in perl flavor:
perl -0040 -ne 'END {print "$x\n"} $x++ if /hello/' /tmp/hh
Fun w/perlrun:
perl -Fhello -0777 -lane 'print $#F' /tmp/hh
# shorter:
perl -apFhello -0777e '$_=$#F' < /tmp/hh
# with check for word boundry (e.g. to exclude "Othello" or mispelled
+"cello")
perl -apF'\bhello\b' -0777e '$_=$#F' < /tmp/hh
| [reply] [d/l] [select] |
|
|
perl -lne '$ct += s/hello//g; }{print $ct' /tmp/hh
;-)
--shmem
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Re: grep command
by Tux (Canon) on Jan 18, 2007 at 13:51 UTC
|
And what if there are more than one occurances of "hello" in one line?
# perl -le'$cnt+=@{[m/\bhello\b/g]}for"aahello hello hello","bb heelo
+hello phello";print$cnt'
3
or, on a file ...
# perl -nle'$cnt+=@{[m/\bhello\b/g]}}END{print$cnt' file
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
|
|
That's the simple modification I told about:
And to answer kyle at Re^2: grep command, my interpretation was that he was concerned about how many times the word happened with no concern for which particular lines. But then he tells about grepping — so if the issue is finding the lines and then telling how many times it appeared in total, this one may work:
| [reply] [d/l] [select] |
|
|
Almost!
However, the last line in which "hello" is found is printed twice.
CLI:
perl -n -e "print, $count++ while /\bhello\b/g; END { print $count }" hello.out
(quotes revised for windows)
file hello.txt:
hello. hellow, world. bye
world says hello.
bye
hello, hellow, hello
not now.
done.
output
hello. hellow, world. bye
world says hello.
hello, hellow, hello
hello, hellow, hello
4
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|