total count of a word in the files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting total count of a word in the files
# 1  
Old 11-21-2011
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 )
Code:
cat test1.txt
himynameisrajandiamfriendofrajeshfriend
wouldyouliketobemyfriend.
 
cat test2.txt
himynameisdostandiamfriendofdostfriend
wouldyouliketobemyfriend.

ouput is:
total number of occurance of friend is: 6

i tried writing a shell script, but it is not working when there is no gap.
Below is the code

Code:
for i in `cat test[1-2].txt`; 
do
if [ "$i" = "friend" ]; then
cnt=`expr $cnt + 1`
fi
done
echo " total number of occurance of friend is: $cnt "

help needed.
# 2  
Old 11-21-2011
Code:
awk '{n+=gsub("friend",x)}END{print n}' test1.txt test.txt

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 12-06-2011
one liner...
Code:
grep -oi "friend" (filesname)  | wc -l


Last edited by Franklin52; 12-06-2011 at 08:22 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

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

3. Shell Programming and Scripting

Finding total count of a word.

i want to find the no:of occurrences of a word in a file 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... (6 Replies)
Discussion started by: mahesh1987
6 Replies

4. Shell Programming and Scripting

Word count in html files

Hi does somebody know how to do a word count in a .html file? Just the text words, without all the html code. Thanks (4 Replies)
Discussion started by: louisJ
4 Replies

5. UNIX for Dummies Questions & Answers

Separate text files in a folder by word count

Hi, been searching Google for like an hour and I doubt I got the right keywords so might as well ask here. What I need: Before: Article1.txt 564 Article2.txt 799 Article3.txt 349 Article4.txt 452 * Separate files over 400 wordcount * After: Article1.txt 564... (3 Replies)
Discussion started by: Richard2000
3 Replies

6. Shell Programming and Scripting

Search and find total count from multiple files

Please advice how can we search for a string say (abc) in multiple files and to get total occurrence of that searched string. (Need number of records that exits in period of time). File look like this (read as filename.yyyymmdd) a.20100101 b.20100108 c.20100115 d.20100122 e.20100129... (2 Replies)
Discussion started by: zooby
2 Replies

7. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

8. UNIX for Dummies Questions & Answers

word count ascii files

how do I display the total number of words in a file which is of the type ascii text (1 Reply)
Discussion started by: madtim
1 Replies
Login or Register to Ask a Question