Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count words in a file??? 2

Status
Not open for further replies.

mremoos

Programmer
Jan 16, 2004
6
PL
Hi, I need to write an awk script that would count the words in a file and print the result. I don't know what kind of command would do that.
I am a beginner and I could really use some help.
Thank you.
 
To count the number of words:
Code:
awk '{t+=NF}END{print t," words"}' /path/to/file
To count the number of time words appear:
Code:
awk '{for(i=1;i<=NF;++i)++w[$i]}
END{for(t in w)print w[t],t}' /path/to/file

Hope This Help
PH.
 
Or

wc -w filename

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Thanks for answering so quickly.
The wc -w command works fine, it's just not exactly what I need.
In fact, I need to count the number of words in a file, ignoring the lines that begin with &quot;;&quot; .
Thanks again.
 
Try

grep -v &quot;^;&quot; filename | wc -w

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
the last one works just fine.
Now, I am still trying to change this one: grep -v &quot;^;&quot; filename | wc -w ,the one i got from user PHV (Thank you!) I want it to ignore the lines that begin with &quot;;&quot; .
Could use some help.
Thanks a lot.
 
Maybe

awk '!/^;/{t+=NF}END{print t,&quot; words&quot;}' /path/to/file


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top