UNIX script to check word count of each word in file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX script to check word count of each word in file
# 1  
Old 11-18-2016
UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file
sample file
Code:
hi how are you
hi are you ok

sample out put
Code:
hi 1
how 1
are 1
you 1
hi 1
are 1
you 1
ok 1

Code:
wc -l filename

is not helping , i think we will have to split the lines and count and then print and also after getting sample output what i will need to do to get below out put if required
Code:
hi 2
how 1
are 2
you 2
ok 1

looking forward to hear from you
# 2  
Old 11-18-2016
Hi,
if you have only one word by line, you could :
Code:
sort -f file | uniq -ic

Regards.
# 3  
Old 11-18-2016
With the input sample and disedorgue's proposal, try
Code:
tr ' ' '\n' <file | sort -f | uniq -ic
      2 are
      2 hi
      1 how
      1 ok
      2 you

# 4  
Old 11-18-2016
Hello mirwasim,

Could you please try following and let me know if this helps you(If you are not worried about the sequence of your strings).
Code:
awk '{++A[$0]} END{for(i in A){print i, A[i]}}' RS='[ |\n]'   Input_file

Output will be as follows.
Code:
are 2
hi 2
how 1
ok 1
you 2

Thanks,
R. Singh
# 5  
Old 11-18-2016
Hi.

Another, similar approach:
Code:
egrep -o '\w+' data1|sort|uniq -c

producing:
Code:
      2 are
      2 hi
      1 how
      1 ok
      2 you

On a system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
egrep grep (GNU grep) 2.20
sort (GNU coreutils) 8.23
uniq (GNU coreutils) 8.23

See man pages for details.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word count files in UNIX

My files: file1 and file2 contain 157 words. Emptyfile1 contain 0 words. My script has to output how many files is filled with words and how many that are not. If there are files that have words in it. It has to output "file2 and file2 contains 157 words, 0 files are empty" If not every... (9 Replies)
Discussion started by: johnrichards
9 Replies

2. Shell Programming and Scripting

Word Count In A Script

I am in need of a basic format to 1. list all files in a directory 2. list the # of lines in each file 3. list the # of words in each file If someone could give me a basic format i would appreicate it ***ALSO i can not use the FIND command*** (4 Replies)
Discussion started by: domdom110
4 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

~~Unix command to count a particular word in the whole directory .~~

Hi , i'm trying to count a particular word occurance in a whole directory..is this possible :wall: say for example there is a directory with 100 files which and all the file may have the word 'aaa' in it ...how would i count the number of 'aaa' in those whole 100 files in a directory ? ... (10 Replies)
Discussion started by: Rabbitsfoot
10 Replies

5. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

6. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

7. Shell Programming and Scripting

how to count a word in a file

dear all, i have a requirement to count the errors and display from a file. eg. file1.txt sjdgfjdgfgd ora-0001 sdjgfydh sdukgh7 23 sjdgfjdgfgd ora-0002 sdjgfydhsf34 ew 34v sjdgfjdgfgd ora-0008 sdjgfydh asdf asdfas sjdgfjdgfgd ora-0001 sdjgfydhjkbs ui873 sjdgfjdgfgd ora-0004 sdjgfydh... (9 Replies)
Discussion started by: unx100
9 Replies

8. UNIX for Dummies Questions & Answers

script to count a word in a flie

hi All, I have one question to have a answer with your help. i need to have a script for searching a file with .log extension form all directories. search for a word " Pass" in the files and to count the occurence of the word "Pass". i need all your help to solve the problem. Thanks,... (3 Replies)
Discussion started by: sipmohan
3 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question