Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to shorten this awk program ... 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
Just wondering,

Can this be simplified?

Code:
awk -F "," 'BEGIN {a=0} $3==1 && $21==1 {a++} END{print a}'
 
I think you can leave out the BEGIN {a=0} because a is incremented in the main program so it is automatically assumed to be an integer which defaults to value 0. Of course you need to print a+0 in the END part on the off chance that no input line has a 1 in fields 3 and 21, otherwise a is assumed to be an empty string.

Also you can leave out most of the spaces and quotes around the comma aren't necessary.

awk -F, '$3==1&&$21==1{a++}END{print a+0}'

or even

awk -F, '$3==$21==1{a++}END{print a+0}'

but neither version helps the readability...



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top