Hi Unix team,
I have a file with 30 columns with tab delimited. I need to count the records based on column 18 & 21 data.
So I cut the data from the file using
awk -F"\t" '{print $18,$21}' foo.txt
Following is the output:
USED SEDAN
USED SUV
NEW SUV
USED Truck
USED Truck
USED SEDAN
USED SUV
NEW Small
Now I need to count the records that are
A. Non SEDAN or Non SUV in $2
B. Records which are USED in $1, but only SEDAN or SUV.
Expected result:
3
4
I tried as:
awk -F"\t" '{print $18,$21}' foo.txt | \
while read line
do
if [ "$2" != "SEDAN" ] && [ "$2" != "SUV" ]
then
((count+=1))
fi
done
Thanks.
Yoda
December 7, 2012, 11:41am
5
awk ' BEGIN { uss=0; nss=0; } {
if($1=="USED" && ($2=="SEDAN" || $2=="SUV")) uss++; if($2!="SEDAN" && $2!="SUV") nss++;
} END {
printf "%d\n%d\n", nss, uss;
} ' infile
3
4
Note: change positions $1 $2
according to your file.
1 Like
rdrtx1
December 7, 2012, 1:28pm
6
try also:
awk '{if ($2!~/SUV/ && $2!~/SEDAN/) {a++} else {if ($1~/USED/) b++}} END {print a; print b}' input
RudiC
December 9, 2012, 4:50am
8
Why don't you do it in one go:
$ awk '{if ($21!~/SEDAN|SUV/) s++; else if ($18=="USED") u++} END{print s;print u}' file
3
4
Note that the matching operator ~
can match a composed regular expression and thus may be faster than running it twice on two strings. If only one single string needs to be found, the immediate string compare ==
will be more efficient.