Insert Columns before the last Column based on the Count of Delimiters

Hi,

I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number.

Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field

awk -F '�' '{ if (NF-1 < 139)} END { "Insert 2 columns before the last Field" }' File.txt

Try (untested):

awk -F� 'NF < 140 {NF+=2; $NF=$(NF-2);$(NF-2)=""}' file

This would add two columns even if there's just e.g. three of them. Is that what you want?

Hi,

Thanks for the reply..

I have tried that but it isn't working.

My requirement is certain records in my file have 139 columns and few others 137. For all the records which have 137 columns , I need to add 2 columns before the last column so that the total count becomes 139

Try:

awk 'NF<c{n=NF; $c=$n; $n=x}1' c=139 FS=� OFS=� file

Hi,
I have executed the command on the file, but when i count the delimiters i still get 138 and 139.Earlier it ised to be 137 and 139.

Ideally I should be getting one sinle value of the delimiters :139.

Also could yo ulet me know the fuctionality of the commad you used.

Many Thanks

c (139) is the number of columns. To get 139 delimiters use c=140 instead of c=139..