本文介绍了awk NF文件名如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

cat testing.txt
==============
line1 1
line2 2 2


line3 3 3

line4

我可以理解awk 'NF > 0' testing.txt的工作方式.该命令仅处理每条记录中具有1个以上字段的字段数,因此将删除空行和制表符.

I can understand how the awk 'NF > 0' testing.txt working. This command is only processing the number of fields with more than 1 field from each record and thus empty lines and tabs are getting removed.

awk 'NF>0' testing.txt
line1 1
line2 2 2
line3 3 3
line4

但是我无法理解awk NF testing.txt的工作方式.它的操作与上述相同.

However I cannot understand how the awk NF testing.txt is working. It is doing the same as above.

awk NF testing.txt
line1 1
line2 2 2
line3 3 3
line4

在这种情况下,我没有指定任何条件.仍然可以正常工作,并从每条记录中删除空行和制表符.

I am not specifying any condition in this case. Still it is working fine and removing empty lines and tab from each record.

我可以在Web上看到很多引用,据说可以使用此命令从文件中删除空行或制表符.仍然无法理解语法.

I can see many references in Web where it is said we can use this command to remove empty lines or tabs from a file. Yet cannot understand the syntax.

推荐答案

NF代表N数量的F ields.因此,每当NF大于0时,该awk就会解释True.所以实际上这是在做

NF stands for Number of Fields. So whenever NF is bigger than 0, that awk interprets True. So in fact this is doing:

if NF>0  --> True
if NF==0 --> False

由于awk的默认行为是{print $0},这意味着它会这样做:

As the default behaviour of awk is {print $0}, this means that it does:

if NF>0   --->  True   ---> {print $0}
if NF==0  --->  False  ---> nothing

所以awk NF file的意思是:打印具有至少一个字段的所有行.这自动意味着:打印所有非空行.

So awk NF file means: print all lines that have at least one field. Which automatically implies: print all no-empty lines.

这篇关于awk NF文件名如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:54