count a occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count a occurrence
# 1  
Old 12-15-2008
count a occurrence

I am looking to get a output of "2 apple found" from the awk command below.

Code:
black:34104 tomonorisoejima$ cat tomo 
apple apple
black:34104 tomonorisoejima$ awk '/apple/ {count++}END{print count " apple found"}' tomo 
1 apple found
black:34104 tomonorisoejima$

# 2  
Old 12-15-2008
Hi! If grep can be use then try this:
Code:
grep apple tomo|wc -w

EDIT: my bad! ignore this suggestion as I tested this with sample file and gives erroneous output.
# 3  
Old 12-15-2008
Hi

Thanks for the answer, but it returns a word count.
I should have given a different file content.

Code:
black:34104 tomonorisoejima$ cat tomo 
tomo apple apple

# 4  
Old 12-15-2008
try this instead
Code:
grep -o apple tomo |wc -l

# 5  
Old 12-15-2008
Oh my God, I never used this -o option.

I was going through awk manual now and this grep option is returning
exactly what I need!

Thanks yongitz, You made my day.

Tomo
# 6  
Old 12-15-2008
Hi.

Create one file say tomo.awk, content is the following

Code:
/apple/{
      for(i=1;i<=NF;i++)
      {
            if($i=="apple")
                  count++
      }
}
END{
      print "Number of apples "count
}

execute the following command

awk -f tomo.awk tomo
(or)
cat tomo|awk -f tomo.awk

Let me know if you got successful.

Thanks,
Shahnaz.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

2. Shell Programming and Scripting

Count of occurrence in particular column of the file.

Hi All, let's say an input looks like: C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 ---------------------------------- 1|0123452|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0156123|C501|X|X|X|E|E|E|E|E|E|E 1|0178903|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0127896|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0981678|C501|X|X|X|E|E|E|E|E|E|E ... (6 Replies)
Discussion started by: suresh_target
6 Replies

3. UNIX for Dummies Questions & Answers

Count occurrence of column3 in column2

count occurrence of column3 in column2 with awk Input: AA BB BB AA BB CC AA BB CC AA CC BB CC CC BB BB Output: AA BB 3 (1 Reply)
Discussion started by: aydj
1 Replies

4. Shell Programming and Scripting

Count occurrence of string in a column using awk

Hi, I want to count the occurrences of strings in a column and display as in example below: Input: get1 345 789 098 get2 567 982 090 fet4 777 610 632 get1 800 544 230 get1 600 788 451 get2 892 321 243 get1 673 111 235 fet3 789 220 278 fet4 768 222 341 output: 4 get1 345 789... (7 Replies)
Discussion started by: aydj
7 Replies

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

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

7. Shell Programming and Scripting

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: (4 Replies)
Discussion started by: Misa-Misa
4 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. UNIX for Advanced & Expert Users

Unix: list out Pattern occurrence (count)

Need to search a pattern occurrence (count) in a specified file. Below is the details $ cat fruits apple apple ball ball apple ball ball ball apple apple apple cat cat cat cat cat apple apple Note: If I'll use the grep command with -c option then it'll count the 1st occurrence in... (6 Replies)
Discussion started by: tp2115
6 Replies

10. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 Replies
Login or Register to Ask a Question