tags:

views:

33

answers:

2

How to print $2 by awk only if the fourth field is not 0 (zero).

line="root     13246 11314  457 15: qsRw -m1"

then awk will print 13246, but if

line="root     13246 11314  0 15: qsRw -m1"

then awk will not print anything

+2  A: 

awk '{if ($4) print $2;}' < inputfile

Chris Card
no need redirection.
ghostdog74
+2  A: 
awk '$4!=0{print $2}' file

or just

awk '$4{print $2}' file

The syntax of awk is

awk '/pattern/{action}' file

the "pattern" part is actually an implicit "if" control flow structure. Therefore you can omit putting an "if" keyword.

ghostdog74
Pedantically, the syntax is `awk 'condition {action}'`, and `/pattern/` is a true/false condition.
glenn jackman