Shell – How to use awk to print only lines containing 5 columns

awkgawkshellshell-script

I have a file which contains pipe delimited data. I want to print the lines which have only 5 columns.
I tried the below. It does not work. It simply prints all the lines. Could you please let me know how to do it correctly. Thank you!

Contents of the file:

10413300|Maintenance Stock Clerk A - Michelle Hanley | Michelle Hanley (1680654)|Filled||10402112
10413301|Maintenance Stock Clerk - dasda|space||10402122
MOM Parkcentrum F;Alkotás u. 53.||34535|34345|asd
Business Center Naberezhnaya Tower" Block C"|||3453|ffg|345

Here I'm trying to print lines which have 5 columns only.

$ gawk -F "|" '{ if (NF=5) print $0 }' data3
10413300 Maintenance Stock Clerk A - Michelle Hanley   Michelle Hanley (1680654) Filled
10413301 Maintenance Stock Clerk - dasda space  10402122
MOM Parkcentrum F;Alkotás u. 53.  34535 34345 asd
Business Center Naberezhnaya Tower Block C   3453 ffg

The output shows that 2 out 4 lines have more than 5 columns.

$ gawk -F "|" '{print NF}' data3
6
5
5
6

Best Answer

NF=5 is an assignment, testing values is done with ==. Like this:

awk -F \| 'NF==5' data3
Related Question