Script to check for a specific number of columns in a file

Hi All

I have a file which has five columns in each rows.

cat file.txt
a|b|c|d|e
1|2|3|4|5
a1|a2|a3|a4|a5
.
.
.

I need to make sure that there are no less than five or more than five columns (in all the rows) by mistake. I tried this :

cat file.txt | awk 'BEGIN{FS="|"};{print NF}' > file1.txt

The above code is giving me the count of fields for each row. Can anybody help me modify it a little more so that the moment for a particular row it does not match five it will print me the row?

Hi,

please try this

nawk 'FS="|" {if(NF!=5)print $0}' file.txt

or

awk -F"|" 'NF!=5 {print $0}' file.txt

venky

1 Like

Using NF is fine, but use it as/in a condition as venky.b5 does, or even as a pattern

awk -F\| 'NF!=5' file

implying the default action {print $0}

2 Likes

Hi All

Thanks! It Worked :slight_smile: