Elliott C. Back: Internet & Technology

Replace, Grep

Posted in Friends by Elliott Back on May 5th, 2005.

Jean issues a challenge:

[ok, now count up the number of times i used the word "think" in this entry]

I respond:

%perl -p -e s/think/think\n/g; < jean-thinks.txt > jean-thinks2.txt

%grep -c think jean-thinks2.txt
14

This entry was posted on Thursday, May 5th, 2005 at 2:11 am and is tagged with lt, perl. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

4 Responses to “Replace, Grep”

  1. stran says:

    %grep ‘think’ jean-thinks.txt | wc -l
    14

    BOOYAH!
    :^)

  2. Sorry stran, that doesn’t work. That will give you:

    %grep ‘think’ jean-thinks.txt | wc -l
    6

    See, grep counts lines, and there are only 6 lines with ‘think’ in them. You really need to split think onto seperate lines first, although the slashes got lost in the post…

  3. Xander says:

    Why not use sed? Perl seems like an overkill. And no need for a temporary file if u pipe it.

    sed s/think/think\n/g jean-thinks.txt | grep think | wc -l

  4. I used temporary files because for some reason piping this stuff on windows doesn’t work properly (i.e., like unix). If I were on linux, Xander, you got it ;)

Leave a Reply

Powered by WP Hashcash