sanku has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
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.

      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

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.

    -derby
      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
        perl -lne '$ct += s/hello//g; }{print $ct' /tmp/hh

        ;-)
        --shmem

        snark the snarkiness. I like it - ++. I hereby ask for forgiveness for all the extra processes I have abused over the years.

        -derby
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

      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:

        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

        I don't see it as a *simple* spoiler, as people quite often try it in scalar context like

        $cnt += m{pattern}g;

        which doesn't work as they expect.

        Enjoy, Have FUN! H.Merijn