LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   add - (minus) in front of a column with value condition of another column (https://www.linuxquestions.org/questions/linux-newbie-8/add-minus-in-front-of-a-column-with-value-condition-of-another-column-4175434761/)

cazanadrian 10-30-2012 10:31 AM

add - (minus) in front of a column with value condition of another column
 
Hello all,

I need some help with a csv file.

My file looks like:

ACL,0,2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0

How I can add - (minus) in front of column 3 if column 5==1

Desired result:

ACL,0,-2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,-6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0

Thanks

David the H. 10-30-2012 01:51 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

This is a perfect job for awk.

Code:

$ awk -F, -v OFS=, '$5 == 1 { $3 = -$3 }1' file.txt
ACL,0,-2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,-6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0



All times are GMT -5. The time now is 10:45 PM.