0

I am counting the number of fields in a row and spit out the row if the number of fields are more than the expected number of fields as below

awk -F”,” ‘{ if ( NF != ‘${x}’ ) print}’ filename

Where x is the expected number of fields. But this is throwing error if the file has more than 100 fields. What is the alternative approach for this.

Sample data:

1,aaaa,bbbb
2,aaaa,bbbb,cccc
3,aaaa,bbbb

Expected output

The line which has more than 3 fields should be displayed

2,aaaa,bbbb,cccc

The main issue here is my file has more than 100 columns and awk is throwing error as it has 100 fields limitation.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
ksp
  • 25
  • 4
  • 2
    All quotes are wrong. Use `"` and `'`. – Cyrus Dec 04 '21 at 20:47
  • 3
    See: [How do I use shell variables in an awk script?](https://stackoverflow.com/q/19075671/3776858) – Cyrus Dec 04 '21 at 20:50
  • In combination to the comment of Cyrus, the curly braces around `$x` lead to a syntax error as it would read as `{ if (NF != {5}) print}` when `x=5`. – kvantour Dec 04 '21 at 21:50
  • @kvantour: Yes, I hadn't noticed that at all. Possibly he/she meant `${x}` instead of `{$x}`. – Cyrus Dec 04 '21 at 21:56
  • Please, post some sample data with the related expected output. Don't post them as comments, images, tables or links to off-site services but use text and include them to your original question. Thanks. – James Brown Dec 04 '21 at 22:03
  • Just edited my question with sample data. Won’t be able to post the real data as the file is more than 2 gb. – ksp Dec 04 '21 at 22:32
  • 1
    Which version of awk are you using and on which operating system are you executing this? – kvantour Dec 04 '21 at 23:09
  • When I do man awk I can see 2010 as the year and is unix – ksp Dec 04 '21 at 23:32
  • See if you have `gawk` installed and use it instead. – Shawn Dec 04 '21 at 23:52
  • Are you by any chance on Solaris and running the default awk from /usr/bin? If so - don't do that as that is old, broken awk. Use /usr/xpg4/bin/awk instead. If that's not the problem then get GNU awk. – Ed Morton Dec 05 '21 at 01:06
  • 1
    Thanks Ed , using the awk in the different path worked fine. Appreciate your help. – ksp Dec 05 '21 at 14:39

1 Answers1

1
$ x=3; awk -F, -v c="$x" 'NF!=c' file

2,aaaa,bbbb,cccc

if your awk has number of field limitation, here is a detour

$ grep -on , file | 
  awk -F: -v c="$x" '{a[$1]++} END{for(k in a) if(a[k]!=c) print k}' | 
  awk 'NR==FNR{a[$0]; next} FNR in a' - file

2,aaaa,bbbb,cccc

the two awk scripts don't parse the large file and should not depend number of fields limitation.

karakfa
  • 66,216
  • 7
  • 41
  • 56