Replace, Grep
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. |

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
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
Sorry stran, that doesn’t work. That will give you:
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…
%grep ‘think’ jean-thinks.txt | wc -l
14
BOOYAH!
:^)