Question

Write an awk script that prints a list of all the words in an input file along with the number of times each word appears.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following awk script which counts the frequency of words from input file.

Program

#this awk script reads every line from input file and counts the frequency
#of it
awk '
{
for (i=1; i<=NF; i++) {
words[$i]++
}
}
END {
printf("Count Word\n----- ----\n")
for (w in words)
printf("%3d %s\n", words[w], w)
} ' $1

Sample Input File:

USER > cat input_words
This is a file with many words.
Some of the words appear more than once.
Some of the words only appear one time
rajini Rajni rajini dix

Sample Output

USER: ./count_words.sh input_words
Count Word
----- ----
1 dix
1 file
2 Some
1 time
1 with
1 once.
2 of
1 This
1 more
1 a
1 Rajni
2 words
1 words.
1 only
1 many
2 the
1 one
2 rajini
1 than
2 appear
1 is

./count_words.sh input words CountWord 1 dix file 2 Some 1 time 1 with once. of 1 This 1 more 1 Rajni 2 words 1 words. only 1

Add a comment
Know the answer?
Add Answer to:
Write an awk script that prints a list of all the words in an input file along with the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT