soemac
December 15, 2008, 4:27am
1
I am looking to get a output of "2 apple found" from the awk command below.
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$
yongitz
December 15, 2008, 4:51am
2
Hi! If grep can be use then try this:
grep apple tomo|wc -w
EDIT: my bad! ignore this suggestion as I tested this with sample file and gives erroneous output.
soemac
December 15, 2008, 4:54am
3
Hi
Thanks for the answer, but it returns a word count.
I should have given a different file content.
black:34104 tomonorisoejima$ cat tomo
tomo apple apple
yongitz
December 15, 2008, 4:59am
4
try this instead
grep -o apple tomo |wc -l
soemac
December 15, 2008, 5:09am
5
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
Hi.
Create one file say tomo.awk, content is the following
/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.