awk if statement

hi,

I have a l-column file of more than 10,000 lines with interspersed negative values. What I want to do is add a fixed number (360) everytime a negative value is encountered while leaving the positive ones as is. I need something that will read every line of the file and do the calculation without changing the order of the data. any idea how to implement this using awk? Many thanks in advance.

sample file looks like the ones below:

original values        desired output
135                    135
123                    123
144                    144
-179                   181 (-179+360)
-164                   196 (-164+360)
156                    156
145                    145
-164                   196 (-164+360)
156                    156
-176                   184 (-176+360)
and so on ..
awk '{ if ($1 < 0) $1 = $1 + 360; print; }' inputfile >outputfile
1 Like

Another way..

awk '$0=($0<0)?$0+360:$0' file
1 Like

Thanks much guys,:slight_smile: