awk +仅当四个字段为0时才打印第二个字段

yae*_*ael 3 awk

如果第四个字段不是0(零),如何通过awk打印$ 2.

line="root     13246 11314  457 15: qsRw -m1"
Run Code Online (Sandbox Code Playgroud)

然后awk将打印13246,但如果

line="root     13246 11314  0 15: qsRw -m1"
Run Code Online (Sandbox Code Playgroud)

然后awk不会打印任何东西

gho*_*g74 12

awk '$4!=0{print $2}' file
Run Code Online (Sandbox Code Playgroud)

要不就

awk '$4{print $2}' file
Run Code Online (Sandbox Code Playgroud)

awk的语法是

awk '/pattern/{action}' file
Run Code Online (Sandbox Code Playgroud)

"模式"部分实际上是一个隐含的"if"控制流结构.因此,您可以省略放置"if"关键字.

  • 小心翼翼地,语法是'awk'condition {action}'`,`/ pattern /`是一个真/假的条件. (2认同)

Chr*_*ard 7

awk '{if ($4) print $2;}' < inputfile
Run Code Online (Sandbox Code Playgroud)