-1

I have a requirement to find value 2 in column 4 and wherever it matches replace 6th column with '-'sign in the beginning

suppose if i have csv file - file1 like this

abc,0,def,1234,ijk
xyz,2,def,2345,abc
ijk,2,def,5678,ijl

I need a regular expression which will provide output like:

abc,0,def,1234,ijk
xyz,2,def,-2345,abc
ijk,2,def,-5678,ijl

How can I do this using awk, sed or perl

1

An awk solution:

$ awk -F',' '$2 == 2 {$4 = "-"$4}1' OFS=',' file
abc,0,def,1234,ijk
xyz,2,def,-2345,abc
ijk,2,def,-5678,ijl

A perl equivalent:

$ perl -F',' -anle '$F[3] = "-".$F[3] if $F[1] == 2; print join ",", @F' file
  • The awk solution worked perfectly.. thank you very much.. – Vino Sep 22 '14 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.