Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top