Finding total count of a word.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding total count of a word.
# 1  
Old 04-01-2013
Finding total count of a word.

i want to find the noSmilief occurrences of a word in a file

Code:
cat 1.txt

unix script unix script 
unix script unix script unix script unix script 
unix script unix script unix
unix
script
unix script unix script

now i want to find , how many times 'unix' was occurred
please help me
thanks in advance.
# 2  
Old 04-01-2013
Try this
Code:
awk '{for(i=1;i<=NF;i++)a[$i]++}END{print a[cnt]}' cnt=unix input_file

--ahamed
# 3  
Old 04-01-2013
One thought

Code:
tr " " "/n" <1.txt | grep "unix" | wc -l

Puts all words onto own lines, changing space to carriage-return
greps for the string
give a line count for matches
# 4  
Old 04-01-2013
If you have GNU grep:
Code:
grep -o "unix" file | wc -l

---------- Post updated at 13:50 ---------- Previous update was at 13:43 ----------

@joeyg, I noticed a typo, also wc -l can be replaced with grep -c switch:
Code:
tr " " "\n" < file | grep -c "unix"

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-01-2013
In case it is undesirable, note that all of the solutions offered will match subword strings.

Regards,
Alister
# 6  
Old 04-01-2013
To handle subwords

Code:
tr " " "\n" <1.txt | grep "^unix$" | wc -l

# 7  
Old 04-02-2013
grep -c word filename
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding total distinct count from multiple csv files through UNIX script

Hi All , I have multiple pipe delimited csv files are present in a directory.I need to find out distinct count on a column on those files and need the total distinct count on all files. We can't merge all the files here as file size are huge in millions.I have tried in below way for each... (9 Replies)
Discussion started by: STCET22
9 Replies

2. UNIX for Beginners Questions & Answers

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 hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

3. UNIX for Dummies Questions & Answers

In ls -l remove total count

Hi All, When i give ls -ltr i get 'total 10' like this along with files long listing. is there any option in ls command to remove this line or do we need use head -1 command only. $ls -ltr total 45 -rw-r--r-- 1 abc g1 0 Jul 17 07:20 0 -rw-r--r-- 1 abc g1 744 May 9 12:10 a -rw-r--r--... (1 Reply)
Discussion started by: HemaV
1 Replies

4. UNIX for Advanced & Expert Users

Finding out total memory usage

Hi, I have a server box with 16GB ram in it, within the server box there are 3 VMs running with a total allocation of 9GB. if I add up all the numbers under memory info using vmstat I get 15.8GB so I can say it adds up to 16Gb... Is there a way to see from the command line how much memory... (2 Replies)
Discussion started by: speedhunt3r
2 Replies

5. Shell Programming and Scripting

total count of a word in the files

Hi Friends, Need help regarding counting the word "friend" in files test1.txt and test2.txt. ( there is no gap/space between word ) cat test1.txt himynameisrajandiamfriendofrajeshfriend wouldyouliketobemyfriend. cat test2.txt himynameisdostandiamfriendofdostfriend... (2 Replies)
Discussion started by: forroughuse
2 Replies

6. Shell Programming and Scripting

Total Count using AWK

Hi Everybody, I have the following example file... 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1 199|TST-GURGAON|GURGAON|1... (8 Replies)
Discussion started by: sraj142
8 Replies

7. 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

8. Shell Programming and Scripting

Finding the total of a column using awk

Here is my file name countries USSR 8650 262 Asia Canada 3852 24 North America China 3692 866 Asia USA 3615 219 North America Brazil 3286 116 South America India 1269 637 Asia Argentina 1072 ... (8 Replies)
Discussion started by: ironhead3fan
8 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question