how to display some special results with AWK

I have a file like this:

AAEQGAGNQPQH,,,,160,32,,,,
AAGQDYNSPLH,,712,39,,,,,,
AAGREGGNTEAF,26,,,,,,,,4
AAGSPQH,,,,8,5,,,,
AAKKLGQFYNEQF,4,,6,,,7,,,2
AANSGGRYNEQF,,2747,3120,,,,,,
AAQGGVGGELF,,,,5,,12,36,,
AAQGLAGYYEQY,,25,13,,,,,,
AAQRGNEQF,510,2,,,6,,6,,76
AAQTGENSPLH,,16,16,,,,,,

The separator is ",". I want to display certain records, the number of whose non-null field is more than 4.

It should be have some simple way to do that, but I can't get what I want.

Thank you!

awk -F, 'gsub(",[^,][^,]*","&") >= 4' myFile
awk -F'[^,]*' 'NF>5' file

or

sed -n 's/[^,]\{1,\}/&/5p' file

or

grep -E '([^,]+(,+|$)){5}' file

The first code will change the number of field, while the last two would not.

When the separator is tab other than "," , what is the code should be? I should learn hard on the regular expression hard.

Thank you !

Hi, the first expression will not change the number of fields, but use any number of non-commas as field separator, so it would do the reverse of what constitutes a field. To do the same with tabs instead of commas, try:

awk -F'[^\t]*' 'NF>5' file

For the other two, it might be that you need to use an actual TAB-character, rather than '\t', which is entered like CTRL-V TAB

Hi vgersh,
nice code :slight_smile:
can you please explain ?

vgersh99's solution can be work like this also..

awk -F, 'gsub(",*,","&") >= 4' file

That's using the return value of gsub (number of substitutions made) to decide whether to print the current record. But, this will print the lines where the first field is null and has exactly 4 non-null fields (as per OP's requirement, number of non-null fields should be more than 4).

Just saw it after doing some modification to the input.
agree with you.. can be lit modified to get desired result.
i am not sure is this best way of doing it or not. just another try...:smiley:

awk -F, 'gsub("[a-z0-9],","&") > 4' file

That will not work correctly for non-empty last fields (and fields with uppercase and/or extended ascii characters :wink: )

This will not count non-empty first fields, no?