Count words from file

hi all
how to count words from a text

aaa bbb ccc ddd
123 aaa 123 aaa
aaa ddd 123

i need to cout hoe many time the words "aaa" and "123" each appears

the output should be

4 3

or

4
3

or

aaa 4
123 3

thanks
Sharon

Hello Sharong,

Kindly use code tags as per forum rules for commands and codes which you are using in your post. Following may help you in same.

awk '{for(i=1;i<=NF;i++){if($i == "aaa"){A++} if($i == "123"){B++}}} END{print "aaa: " A ORS "123: " B}'  Input_file

Output will be as follows.

aaa: 4
123: 2

Thanks,
R. Singh

Is this homework?

you can easely create a histogram with:

cat <file.txt> | tr ' ' '\n' | sort | uniq -c | sort -rn 

this will give you the count for any words in input file.
if you just want a specific word , grep for it !
hope it helps.

Please no more posts till user has replied to the asked question
Thanks for your understanding

no homework , just work :):slight_smile:

Hi,

Please specify what have you done to solve this by yourself , it would avoid people being suspicious :slight_smile:
there are many ways to find it, try this one:

grep -o -c 'aaa' <FileName>; grep -o -c '123'  <FileName>

[LEFT]I have an output from a router regarding the amount of license comparison
To the number of cards I need to count number of card (3g60) and get to output
And the number of port (SUBSLOT) and get the number[/LEFT]

[LEFT]I have an output from the router and I don't want to put it in a file

 
telnet $IPCMTS 2>> $LDIR/errors.txt | egrep "Subslot|MC3GX60V" |  grep -o -c 'Subslot'   |  grep -o -c 'MC3GX60V' | tee -a  $LDIR/res1.txt

[/LEFT]

I'm pretty sure all you get from above is 0 .
What shell do you use? In a recent bash with process substitution, try sth like

telnet $IPCMTS 2>> $LDIR/errors.txt |  tee >( printf "%s %s\n" 'subslot' $(grep -o -c 'Subslot'))|  tee >( printf "%s %s\n" MC3GX60V $(grep -o -c 'MC3GX60V')) 
subslot 3
MC3GX60V 4

Be careful as the output will not be synchronized with your shell session.

1 Like

thanks RudiC

with advice of a colleague I put the output in to a file an count it