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

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

awk -F \| 'NF==5' data3
| improve this answer | |
  • 2
    Confusing assignment and comparison operators is one of the most-common problems in "programming" (in the broadest sense of the term). It's very easy, especially in a *nix command-line environment, where Bourne shell and descendants interpret Variable=value as assignment but Variable = value as comparison – Monty Harder Jan 31 '17 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.