How to count the number of occurrence of words from multiple files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count the number of occurrence of words from multiple files?
# 1  
Old 06-06-2012
Question How to count the number of occurrence of words from multiple files?

File 1

aaa
bbb
ccc

File 2

aaa
xxx
zzz
bbb

File 3

aaa
bbb
xxx

Output:

aaa 3
bbb 3
ccc 1
xxx 2
zzz 1

Thanks~
# 2  
Old 06-06-2012
Code:
perl -ne 'chomp; $x{$_}++; END{for(sort keys %x){print "$_ -> $x{$_}\n"}}' file1 file2 file3

# 3  
Old 06-06-2012
Code:
sort File* | uniq -c

i get it. XD

Last edited by Franklin52; 06-06-2012 at 04:18 AM.. Reason: Please use code tags, thank you
# 4  
Old 06-06-2012
Try this one
Code:
cat File* |sort |uniq -c

# 5  
Old 06-06-2012
awk

Hi,

You can try this too Smilie

Code:
awk '{a[$0]++;}END{for(i in a){print i"="a[i];}}' File*

Thanks,
RangaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

2. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

3. Shell Programming and Scripting

problem to count number of words from file

hi every one i have written this simple shell for counting number of word that user need to find from file but i have get several error when run it. can someone tell me the problem ? echo "Enter the file name" read file echo "enter word" read word for i in \`cat $file` do if then... (1 Reply)
Discussion started by: nimafire
1 Replies

4. Shell Programming and Scripting

Count the number of occurances for multiple files

I have some text files as shown below. I would like to add the values of each string. Your help would be appreciated!! file1.txt SUS 2 PRS 2 ALI 1 PRS 1 GLK 2 file2.txt PRS 3 GLK 6 SUS 18 Desired output SUS 20 PRS 6 (2 Replies)
Discussion started by: arch
2 Replies

5. Shell Programming and Scripting

How to count words number for each paragraph?

Hi, I have file like this: >number1 dlejreoreltedgerere dgtrtryr >number2 dkfdjlgdjfeiflefjdfidlfjdifelfjefe fjdlfjdkfjdlfdjlfdjlfjdigeo gjelreoureofouererg >number4 dklfdjfoeueoruer fjeorueotueoirueorueorueore gjoeoueorueoreuroerueor joeuroerueorue How can i know how many words... (4 Replies)
Discussion started by: the_simpsons
4 Replies

6. Shell Programming and Scripting

How to count the occurrence of a number?

Hi, I have a file which contained a set of numbers like Col1 col2 col3 col4 1 sa 13 0 2 sb 14 0 3 sc 15 9 4 sd 16 -9 5 sd 20 -2 6 sd 20 4 Here in last column I need to count the zeros, positive values and negative values, please help me to do that. (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

7. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

8. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

9. Shell Programming and Scripting

Count number of match words

Input: some random text SELECT TABLE1 some more random text some random text SELECT TABLE2 some more random text some random text SELECT TABLE3 some more random text some random text SELECT TABLE1 some more random text Output: 'SELECT TABLE1' 2 'SELECT TABLE2' 1 'SELECT TABLE3' 1 I... (5 Replies)
Discussion started by: chitech
5 Replies

10. Shell Programming and Scripting

Count the number of words in some subset of file and disregard others

Hi All, I have some 6000 text files in a directory. My files are named like 1.txt, 2.txt 3.txt and so on until 6000.txt. I want to count the "number of words" in only first 3000 of them. Any suggestions? I know wc -w can count the number of words in a text file. I am using Red Hat Linux. (3 Replies)
Discussion started by: shoaibjameel123
3 Replies
Login or Register to Ask a Question