awk delimiter count if mismatched?

Hi all,

I have a file where in it has lot of records in it.
I have written below stuff to find the number of fields as shown below

`awk -F '|' '{print NF-1}' file.txt| head -1`

[/CODE]

how do i proceed if in case any record in particular is having more number of delimiters, if it having??? what is the record content (need to print that particular record)?

for example

I have content of table

id|name|desc
1|Raj|null|
2|Nick|NA
0|Digu|fine with it
3|NA|go ahead
0|Digu|fine with it
100|Digu|fine with it
4|Duvi|baby

[/CODE]

My o/p should be

1|Raj|null|

[/CODE]

as it is having extra delimiter

Try:

awk -F '|' 'NR==1{n=NF}NF>n' file

or if it is either less or more:

awk -F '|' 'NR==1{n=NF}NF!=n' file
1 Like

thanks a lot :slight_smile: how can i ignore the escaped delimiter

for ex

2|Nick|NA\| 

[/CODE]

It should ignore this kind as there are chances that "|" is part of the record as well not only delimiter.

Try

awk -F '|' 'NR==1 {n=NF} NF-gsub(/\\\|/, "&")!=n' file
1|Raj|null|
1 Like

thanks a lot sir :slight_smile:

Hello Nikhil,

In case you need only those lines which are having more number of delimiters following may help you. Taken code from RudiC's suggestion with a minor edit in it as follows.

awk -F '|' 'NR==1 {n=NF} NF-gsub(/\\\|/, "&")>n'  Input_file

So let's say Input_file is as follows:

cat Input_file
id|name|desc
1|Raj|null|
2|Nick|NA
0|Digu|fine with it
3|NA|go ahead
0|Digu|fine with it
100|Digu|fine with it
4|Duvi|baby
4|Duvi
 

Then only line 1|Raj|null| will be printed by above code, hope this helps you too.

Thanks,
R. Singh

1 Like

thanks a lot ravinder :slight_smile:

One more thing, I will validate the number of delimiters based on the very first row of the file that is the header. So can we include that as well in the check with the script u gave.

That's in there from the very first scriptlet by Scrutinizer.