Hi all,
I have a pattern like this in a file:
123 4 56 789
234 5 67 789
121 3 56 789
222 4 65 789
321 6 90 100
478 8 40 789
243 7 80 789
How can I count the number of occurences of '789' (4th column) in this set...?
Thanks for all your help!
K
I gave it like this... Can anyone say me if this is correct!!!
cat <file> | awk '{print $4}' | grep '789' | wc -l
Please say me if there is any other easy way of doing this!
Thanks
shorter version of the above suggestion..
awk '/789/ {print $4}' file|wc -l
Even shorter:
grep -c '789' file
(Disclaimer: this fails for files with '789' showing up in columns 1 to 3 as well.)
not all the time! if the 1st column has the 789 then your logic will go wrong..
::EDIT:: Haven't read your disclaimer at first..
awk '{a[$4]++}END{print a[789]}' file
#! /usr/bin/perl
$n=0;
open FH,"a.txt";
while(<FH>){
my @tmp=split(" ",$_);
my @arr=grep {$_==789} @tmp;
$n+=$#arr+1;
}
print $n;
awk '{num=gsub("789","",$0);n+=num}END{print n}' a.txt