Searching comma's

I have a delimited file. Each line consists of 5 fields separated by commas.so each line should consists of 4 commas. But due to some quality issue few lines consists of extra comma. need to print those odd lines.
need an unix command to find out those lines

100, Peter,12, 24 cromwell street, Mgr
101, Tom,12, 24 cardington square, Supervisor
103, Bob,12, 23 Beaver,cres, Steward

Hi, try:

awk -F, 'NF>5' file

It prints lines where the number of fields ( NF ) is more than 5, using a comma as a field separator ( -F, )

1 Like

Hello pranabpal,

It would have been better to show what you had tried so we can see your preferred style/tools etc. That way we would find a solution that you could understand better when you come back to the code in a few years time for any reason.

Do you have a plan for how you would achieve this with processes you already know? We could help adapt them.

In any case, another option (and there will be plenty more:-

egrep "(.*,){5}" file

This will find any rows with 5 commas, however rows with more commas are also collected because the search for 5 is contained within them.
If you want to find rows with fewer that 4 delimiters, try this variation:-

egrep -v "(.*,){4}" file

This will find any rows with four commas and then the -v flag will exclude them, so you get anything less than four reported.

I hope that these help,
Robin

Thanks