How to count number of occurances of string in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count number of occurances of string in a file?
# 1  
Old 10-13-2010
How to count number of occurances of string in a file?

Gurus,

Need little guidance.
I have A.txt and B.txt file. B.txt file contains Unique strings.
Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file
Quote:
abc
vbn
ghjjj
jhk:nbv:ghjjj
And A.txt file contains the above string as a fourth column which is last column.
So A.txt file fourth column content is
Quote:
abc
abc
abc
vbn
ghjjj
jhk:nbv:ghjjj
Code:
while read line
    do
    COUNT=`grep "$line" A.txt | wc -l`
    echo $line "," $COUNT >> output.txt
    done < B.txt

Output.txt file displays output
Quote:
abc 3
vbn 1
jhk:nbv:ghjjj 1
ghjjj 2
Here ghjjj should display 1 instead 2...
Can anybody guide me the better way to get this.

Thanks in advance!!!
# 2  
Old 10-13-2010
Take a look at the man pages of command "uniq" and "comm"
Code:
uniq -c A.txt

Note that this assume A.txt has previously been sorted.
Code:
sort A.txt | uniq -c


Last edited by ctsgnb; 10-13-2010 at 05:42 AM..
# 3  
Old 10-13-2010
Hi,

I would change a little bit your script, test this:

Code:
#!/bin/sh

while read line; do
    count=$(grep "^${line}$" a.txt | wc -l)
    echo "$line $count"
done < b.txt

Regards,
# 4  
Old 10-13-2010
Quote:
Originally Posted by Shirisha
Gurus,

Need little guidance.
I have A.txt and B.txt file. B.txt file contains Unique strings.
Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file

And A.txt file contains the above string as a fourth column which is last column.
So A.txt file fourth column content is


Code:
while read line
    do
    COUNT=`grep "$line" A.txt | wc -l`
    echo $line "," $COUNT >> output.txt
    done < B.txt

Output.txt file displays output

Here ghjjj should display 1 instead 2...
Can anybody guide me the better way to get this.

Thanks in advance!!!
That's because in A.txt, the fourth line also contains "ghjjj", so it greps 2
So with awk...
Code:
awk 'NR==FNR{arr[$0]++;next}arr[$0]{print $0,arr[$0]}' A.txt B.txt

# 5  
Old 10-13-2010
You can modify your script like this:
Code:
while read -r line
do     
  COUNT=$(grep -Fcx "$line" A.txt)
  echo "$line , $COUNT"
done < B.txt > output.txt

It is similar to birei's suggestion

Last edited by Scrutinizer; 10-13-2010 at 06:36 AM..
# 6  
Old 10-13-2010
Quote:
Originally Posted by malcomex999
That's because in A.txt, the fourth line also contains "ghjjj", so it greps 2
So with awk...
Code:
awk 'NR==FNR{arr[$0]++;next}arr[$0]{print $0,arr[$0]}' A.txt B.txt

yeah its correct but the above solution is not working...

---------- Post updated at 05:11 AM ---------- Previous update was at 04:59 AM ----------

Quote:
Originally Posted by birei
Hi,

I would change a little bit your script, test this:

Code:
#!/bin/sh

while read line; do
    count=$(grep "^${line}$" a.txt | wc -l)
    echo "$line $count"
done < b.txt

Regards,
hi..
I am getting 0 for every line....i thing that grep wc -l is not working...
i tried giving in ` ` but giving error that command not found
# 7  
Old 10-13-2010
Quote:
Originally Posted by Shirisha
yeah its correct but the above solution is not working...

---------- Post updated at 05:11 AM ---------- Previous update was at 04:59 AM ----------
What error do you get when you run it?

Use nawk if you are on Solaris...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep pattern file and count occurances

Guys I am trying grep to read from pattern file and count occurances of each word. input is firstplace secondplace secondpot patternfile is place first second i want the following. 1-count number of times keywords in patternfile occurs in begining of each line in input file. so... (12 Replies)
Discussion started by: ahfze
12 Replies

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

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

4. Shell Programming and Scripting

Count occurances of X Y Z in a file in 1 go.

Hi. I need to count multiple occurrences of X Y Z in a file in 1 go. At the moment I have the following scripts: ssh readonly@$ServerIP 'YEAR=xx;DAY=xx;MONTH=xx;LMONTH=xx;for i in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 \ 16 17 18 19 20 21 22 23; do cat... (13 Replies)
Discussion started by: msullivan
13 Replies

5. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

6. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies

7. UNIX for Dummies Questions & Answers

Count occurances in a file

Hi, I have a fixed width file in the following format Sr.No A.No Name 1 2 PPP 3 4 PPP 5 6 TTT 7 8 OOO 9 10 OOO 11 12 OOO The 3rd column starts at position 10 and ends at 15. I want to count the number of occurances of each Name and output to a file Example in the... (2 Replies)
Discussion started by: samit_9999
2 Replies

8. Shell Programming and Scripting

Count occurances of a character in a file

I want to find the number of occurences of a character in a file. How do i do it. Eg: $cat file1.txt Welcome to World of Unix. $ If i want to find the occurences of 'o' then I should be getting 3. Thanks. (6 Replies)
Discussion started by: Shivdatta
6 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

count the number chracters occurances in a line

Hi Could anybody tell me how to count the number of occurances of a character within a LINE. actually i have a single line with words seperated with '+' chracter e.g. abc+def+ghj+jkl+asd i want to separte the words above. Please provide the necessary logic in the form of a SHELL... (6 Replies)
Discussion started by: skyineyes
6 Replies
Login or Register to Ask a Question