Inserting string in between field in comma separated file

Hello Mates,

I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G

SE18 6RN,,,,5439070,1786840,,1000002148671600,123434

Out put should be:

SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434

Can some one help me to do?

Thanks

$ nawk -F, 'BEGIN{OFS=","}{$(NF-2)="FALSE";print}' input.txt
SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434
perl -F, -ane '($F[-3] eq "")&&($F[-3]="FALSE");print join(",",@F)' inputfile
printf ",s/[^,]*,/FALSE,/7\nw\nq\n" | ed -s yourfile

Thanks a lot all,

Hi itkamaraj , balajesuri,

As I am very expert I just want to know that can you please give brief how your soultion works i.e Exactly how this works:

1. nawk -F, 'BEGIN{OFS=","}{$(NF-2)="FALSE";print}'
2. perl -F, -ane '($F[-3] eq "")&&($F[-3]="FALSE");print join(",",@F)' inputfile
 

Note: I am using HP unix 11.1 not linux.

Many thanks
Krsnadasa

---------- Post updated at 02:20 AM ---------- Previous update was at 02:19 AM ----------

Sorry Mates I mean to say I am not very expert in Unix..

:wink:

---------- Post updated at 05:19 AM ---------- Previous update was at 02:20 AM ----------

Thanks a lot all,

Hi itkamaraj , balajesuri,

As I am not very expert I just want to know that can you please give brief how your soultion works i.e Exactly how this works:

Code:

  1. nawk -F, 'BEGIN{OFS=","}{$(NF-2)="FALSE";print}'2. perl -F, -ane '($F[-3] eq "")&&($F[-3]="FALSE");print join(",",@F)' inputfile

Note: I am using HP unix 11.1 not linux.

 nawk -F, '{$(NF-2)="FALSE"; print}' OFS=, input.txt

-F,     => Indicating the field separator is , (comma)
NF      => The total no of fields
NF-2    => Second last field
$(NF-2) => The value of second last field. $(NF-2)="FALSE" means assign "FALSE" to the second last field
OFS=,   => Assign output field separator ,(comma).
print   => Print

HTH
--ahamed

Many thanks Ahamed,

Your soultion is wokring fine. Sorry for delay in reply.

Hi itkamaraj ,

Your perl is not doing anything atcually.

Thanks
Krsnadasa

It's Larry Wall's Perl, not itkamaraj's :smiley:
By the way, itkamaraj posted a solution in nawk.

And the Perl one-liner works for me. I'm using GNU bash, version 3.1.17 and Perl v5.8.8.

# cat input
SE18 6RN,,,,5439070,1786840,,1000002148671600,123434
#
#  perl -F, -ane '($F[-3] eq "")&&($F[-3]="FALSE");print join(",",@F)' input
SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434

Hello All,

Thanks perl is also working fine.

Thanks to resolve the issue.

Closing the thread