I have the following file:

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


line3 3 3

line4

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

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.

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 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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top