in reply to Re: grep command
in thread grep command

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:

$ perl -n -e 'print, $count++ while /\bhello\b/g; END { print $count } +' text.out

Replies are listed 'Best First'.
Re^3: grep command
by ww (Archbishop) on Jan 18, 2007 at 16:47 UTC
    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

Re^3: grep command
by Tux (Canon) on Jan 18, 2007 at 14:12 UTC
    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
      I don't see it as a *simple* spoiler, as people quite often try it in scalar context like
      $cnt += m{pattern}g;

      But then the usual workaround is

      $cnt += () = m{pattern}g;

      which is somewhat simpler and more natural than your reference-and-dereference trick.