UNIX question--please help!!!

aircasper

Senior member
Jan 26, 2001
486
0
0
i don't know where else to turn to at this point, so i thought i'd turn to the helpful (and knowledgeable) anandtech community. my wife is taking a unix class (don't know why you need that for a PhD in Sociology) and she needs to figure out what command to use in order to count how many times a specific word appears in a file.

i guess the grep -c command is not what she needs, since that only counts how many lines the word appears in, as opposed to how many times the word appears in the file (i.e., accounting for multiple occurrences of a word within a single line). i've been all over the web with google trying to find an answer, with no luck. any thoughts guys and gals????

thanks in advance!
 

Talon

Golden Member
Oct 29, 1999
1,426
0
0
something like this:

grep the_word_being_searched | wc

use grep to search for the specific word, pipe to wc for word count

scroll down to pipes and redirection at

this site. It has a better explanation.
 

aircasper

Senior member
Jan 26, 2001
486
0
0
thanks talon! my wife doesn't want me to forward her the answer yet, as she wants to try and figure it out. but i gave her the hint about piping and redirection based on your help. it's kind of funny how i'm the one who's more into computers and such, but my wife (who is pratically anti-computer) is turning into the unix geek (not on her own accord of course). ;)


hmm, it doesn't seemed to have worked. my wife said that was actually one of the first things she tried. apparently, it gives her a word count for the lines that contain the word she's looking for, rather than telling her the number of times the specific word appears.
 

KK

Lifer
Jan 2, 2001
15,903
4
81
Originally posted by: Talon
something like this:

grep the_word_being_searched | wc

use grep to search for the specific word, pipe to wc for word count

scroll down to pipes and redirection at

this site. It has a better explanation.

Will this give you a word count for the lines that have the searched word in them and not of how many time the searched word was found.

KK
 

Talon

Golden Member
Oct 29, 1999
1,426
0
0
KK may be correct. So much for that other site I posted a link to. :eek:
 

aircasper

Senior member
Jan 26, 2001
486
0
0
yeah, unfortunately it didn't work. but thanks for the help talon. :) any other thoughts?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
perl -e 'open F, "filename.txt";while(<F> ) {while ($_ =~ /word/g){$i++}}print "$i\n";'

replace "filename.txt" with the name of your file and "word" with the word you're searching for.
 

Legendary

Diamond Member
Jan 22, 2002
7,019
1
0
Originally posted by: notfred
perl -e 'open F, "filename.txt";while(<F> ) {while ($_ =~ /word/g){$i++}}print "$i\n";'

replace "filename.txt" with the name of your file and "word" with the word you're searching for.

This must be the simplest Perl possible because I actually understand what it means without ever seeing Perl before.
/me hands himself a cookie. :D
 

polm

Diamond Member
May 24, 2001
3,183
0
0
Originally posted by: polm
cat file |fmt -w1 |grep word |wc -l

replace file with actual filename
replace word with the word you want a count of
 

wyvrn

Lifer
Feb 15, 2000
10,074
0
0
Not wc -l , (-l counts lines, not words).

grep -o -i <word> <file> | wc -w

what will appear on the screen is the number of times the word was found
 

polm

Diamond Member
May 24, 2001
3,183
0
0
Originally posted by: wyvrn
Not wc -l , (-l counts lines, not words).

grep -o -i <word> <file> | wc -w

what will appear on the screen is the number of times the word was found

that is why I included the | fmt -w1 |
 

The Dancing Peacock

Diamond Member
Dec 22, 1999
3,385
0
0
Originally posted by: polm
Originally posted by: polm
cat file |fmt -w1 |grep word |wc -l

replace file with actual filename
replace word with the word you want a count of

This didn't work for me.

I have a file called hungup with about 20 lines, each with having the word root in the line, and here was my output

cat hungup |fmt -w1|grep root|wc -l
fmt: bad width: 0
0

 

Chaotic42

Lifer
Jun 15, 2001
35,216
2,359
126
Originally posted by: The Dancing Peacock
This didn't work for me.

I have a file called hungup with about 20 lines, each with having the word root in the line, and here was my output

cat hungup |fmt -w1|grep root|wc -l
fmt: bad width: 0
0

I made a file called rootfile, put 20 "roots" in it, one on each line, and here's what I got.
/tmp$ cat rootfile |fmt -w1 |grep root |wc -l
20

Do you have the spaces in the right places?

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Legendary
Originally posted by: notfred
perl -e 'open F, "filename.txt";while(<F> ) {while ($_ =~ /word/g){$i++}}print "$i\n";'

replace "filename.txt" with the name of your file and "word" with the word you're searching for.

This must be the simplest Perl possible because I actually understand what it means without ever seeing Perl before.
/me hands himself a cookie. :D

So could you explain to me what it does,then? ;)


It appears that my perl script is the only thing that works so far :)
 

polm

Diamond Member
May 24, 2001
3,183
0
0
Originally posted by: Chaotic42
Originally posted by: The Dancing Peacock
This didn't work for me.

I have a file called hungup with about 20 lines, each with having the word root in the line, and here was my output

cat hungup |fmt -w1|grep root|wc -l
fmt: bad width: 0
0

I made a file called rootfile, put 20 "roots" in it, one on each line, and here's what I got.
/tmp$ cat rootfile |fmt -w1 |grep root |wc -l
20

Do you have the spaces in the right places?

 

Rias

Member
Aug 23, 2002
101
0
0
Originally posted by: notfred
perl -e 'open F, "filename.txt";while(<F> ) {while ($_ =~ /word/g){$i++}}print "$i\n";'

replace "filename.txt" with the name of your file and "word" with the word you're searching for.

cat filename.txt | perl -nle 'while (m/\bword\b/ig) { $i++ } END { print $i }'

or

cat filename.txt | perl -nle '$i++ while m/\bword\b/ig; END { print $i }'

will only catch if the word is "word" not "wordplay" , and it's case insensitive.
 

aircasper

Senior member
Jan 26, 2001
486
0
0
wow, a lot of suggestions here. i'll have my wife try them out when we get home (stuck in the library stacks right now). thanks everyone!