Counting the records based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting the records based on condition
# 1  
Old 12-07-2012
IBM Counting the records based on condition

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
Code:
 
awk -F"\t" '{print $18,$21}' foo.txt

Following is the output:
Code:
 
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.
Code:
 
Expected result:
3
4

I tried as:
Code:
 
awk -F"\t" '{print $18,$21}' foo.txt | \
while read line
do
if [ "$2" != "SEDAN" ] && [ "$2" != "SUV" ] 
then
((count+=1))
fi
done

Thanks.

Last edited by karumudi7; 12-07-2012 at 12:17 PM..
# 2  
Old 12-07-2012
Is this homework?
# 3  
Old 12-07-2012
Quote:
Originally Posted by Corona688
Is this homework?
No it's not a homework. Smilie
# 4  
Old 12-07-2012
Code:
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.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-07-2012
try also:
Code:
awk '{if ($2!~/SUV/ && $2!~/SEDAN/) {a++} else {if ($1~/USED/) b++}} END {print a; print b}' input

# 6  
Old 12-09-2012
Quote:
Originally Posted by bipinajith
Code:
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.
Thanks.
# 7  
Old 12-09-2012
Why don't you do it in one go:
Code:
$ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

2. Shell Programming and Scripting

Deleting the records based on the condition

Hi, Can any one help me, in deleting the records from the database table based on the following condition: script should take a configurable parameter as input. The input is nothing but “no. of years”. For example, if I enter 2 as input parameter, then the 2 year old records should get... (2 Replies)
Discussion started by: zxcjggu708
2 Replies

3. UNIX for Dummies Questions & Answers

Delete records from a big file based on some condition

Hi, To load a big file in a table,I have a make sure that all rows in the file has same number of the columns . So in my file if I am getting any rows which have columns not equal to 6 , I need to delete it . Delimiter is space and columns are optionally enclosed by "". This can be ... (1 Reply)
Discussion started by: hemantraijain
1 Replies

4. Shell Programming and Scripting

Counting number of records with string row delimiter

HI, i have a file like this t.txt f1|_f2|_ f1|_f2|_ f1|_f2|_ as if col delimiter is |_ and row delimiter |_\n trying to count number of records using awk $ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt 7 wondering how can i count this to 3 ? thx (9 Replies)
Discussion started by: aksforum
9 Replies

5. Shell Programming and Scripting

Counting under some condition

I want to develop a shell script but with a bit complicated conditions. I have a file contains a data. The data change every 5 mins. If the file having data repeated 3 times then add 5 to the counter. By using crontab I will let the script check the file again and if the same 3 data are exist... (2 Replies)
Discussion started by: Bluetoot
2 Replies

6. Shell Programming and Scripting

Averaging all fields while counting repeated records

Hi every one; I have a 31500-line text file upon which two following tasks are to be performed: 1: Rearranging the file 2: Taking the average of each column (considering number of zeros) and output the result into a new file This is the code I've come up with: awk '(NR%3150<3150)... (0 Replies)
Discussion started by: nxp
0 Replies

7. Shell Programming and Scripting

Help me in counting records from file

Hi, Please help me in counting the below records(1st field) from samplefile: Expected output: Count Descr ------------------------------------------- 7 Mean manager 14 ... (7 Replies)
Discussion started by: prashant43
7 Replies

8. Shell Programming and Scripting

Counting records with AWK

I've been working with an awk script and I'm wondeing id it's possible to count records in a file which DO NOT contain, in this instance fields 12 and 13. With the one script I am wanting to display the count for the records WITH fields 12 and 13 and a seperate count of records WITHOUT fields... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

9. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question