Awk + print the second field in the line only if four fields are 0

How to print $ 2 on 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 doesn't print anything

+3
source share
2 answers
awk '{if ($4) print $2;}' < inputfile
+6
source
awk '$4!=0{print $2}' file

or simply

awk '$4{print $2}' file

The awk syntax is

awk '/pattern/{action}' file

the "template" part is actually an implicit if control. Therefore, you can omit the "if" keyword.

+12
source

Source: https://habr.com/ru/post/1750661/


All Articles