Count when meet requirement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Count when meet requirement
# 1  
Old 01-16-2014
Count when meet requirement

I have my file input

Code:
Land,A,091374346294,Cathay,165
Island,B,091370291502,Cathay,3325
Island,P,091366545904,Cathay,440
Island,C,091368476591,Cathay,99000
Land,A,091379924879,Cathay,0
Land,P,091378222275,Cathay,245
Water,X,091369911459,Cathay,0
Island,B,091377596759,Cathay,0
Island,P,09137679859,Cathay,80000
Island,B,091355566759,Cathay,0

I need to count all lines when when column 5 = 0

I expect the output

Code:
Island,B,2
Land,A,1
Water,X,1

I run this

Code:
awk '{s=0; for(i=5; i<=NF; i++) if ($i ="0") s++; print $1,$2,$4,s}' FS=, OFS=, input

but the output is always 0

Last edited by radius; 01-16-2014 at 11:50 PM..
# 2  
Old 01-17-2014
You might want to try something like:
Code:
awk '
BEGIN { FS = OFS = ","
}
$5 == 0 {
        c[$1 FS $2]++
}
END {   for(i in c) print i, c[i]
}' input

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-17-2014
it works..thanks

please do me a favour to explain this code
Code:
c[$1 FS $2]++

huge thanks
# 4  
Old 01-17-2014
Thanks Don for great code. Not good code like Don's,but may help.

Code:
awk -F"," '$5==0 {a=$1OFS$2OFSb; b++; print a","b}; b=0' OFS=, file_check | awk -F"," '{count[$1","$2]+=$3;} END{for(i in count)  print i" "count[i]}' OFS=,

Output will be as follows.

Code:
Land,A 1
Island,B 2
Water,X 1


Thanks,
R. Singh
# 5  
Old 01-17-2014
Quote:
Originally Posted by radius
it works..thanks

please do me a favour to explain this code
Code:
c[$1 FS $2]++

huge thanks
Don might be busy

c[..]- -> Array

$1 FS $2 --> index fileld1 and field2 separated by field separator

Array c holds a count of occurrences of index $1 FS $2

Example :
Code:
$ cat <<test | awk '{c[$1]++}END{for(i in c)print "index",i,"is repeated",c[i],"times"}'
1
1
2
2
3
3
3
test

index 1 is repeated 2 times
index 2 is repeated 2 times
index 3 is repeated 3 times

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 01-17-2014
If column 5 is the end of the line, could you not just:-
Code:
grep -c ",0$" file

The expression is to search for a comma, a zero, then the end of the line.



I hope that this helps.



Robin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk do not split if condition is meet

Trying to use awk to format the input based on the filed count being 5. Most lines are fine using the awk below, except the first two lines. I know the reason is the -1 in green and -2 in blue. But can not figure out how to not split on the - if it is followed by a digit then letter. Thank you :).... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

Print lines meet requirement

Dear Masters, I have 2 files input below file1 8269229289|CROATIA|LUX 8269229412|ASIA|LUX 8269229371|EUROPE|LUX 8269229355|LANE|LUX 8269229469|SWISS|LUX 8269229477|HAMBURG|LUX 8269229484|EGYPT|LUX 8269229485|GERMANY|LUX 8269229498|CROATIA|LUX File2 8269229289|1100100020... (6 Replies)
Discussion started by: radius
6 Replies

3. What is on Your Mind?

Where did you meet UNIX for a first time?

Simple question , where did you meet UNIX OS-es. I started with linux, and then I have meet Solaris and I am all in Solaris right now , almost a year that I am in UNIX, still reading manuals. (35 Replies)
Discussion started by: solaris_user
35 Replies

4. Shell Programming and Scripting

Requirement

I am trying to script and came up with a conclusion that I need a do while loop in my statement. I am stuck with the do while syntax. I need to use it alongwith the if then else statement. Can I use it is a big question? I actually need to get all the files that are there from within run_dt to... (1 Reply)
Discussion started by: aronmelon
1 Replies

5. Shell Programming and Scripting

Need to meet anbu23

hi anbu23 u have just replied to my post i want to ask u something else can we get into contact thanks or reply itself mv -f $XXTEST_TOP/install/SPE1/XXTEST_SPE1_XX_QUOTE_DETAILS_TBL.sql $XXTEST_TOP/admin2/sql if then echo "Move is successful" fi 1. What u mean by $? here can u be... (1 Reply)
Discussion started by: Lutchumaya
1 Replies
Login or Register to Ask a Question