简体   繁体   中英

Using awk to sum an integer to a column in bash

I have a file (tmp0.tmp) of one column with negative and positive values, like this:

-109.372
-152.846
121.435
122.107
-1.172
-118.116

I want to sum 360 to each negative value, and keep the positive ones in the same position.

I did:

for file in tmp0.tmp
do
awk '{if ($1 < 0) {print $1+360} elseif {print $1}' $file > histogram.dat
done

and it didn't work.

Can anyone help me please?

Thanks

You can do it using awk , no need use loops

awk '$1<0{$1+=360}1' inputfile
250.628
207.154
121.435
122.107
358.828
241.884

Just another way with bash + bc :

while read -r n; do echo "if ($n<0) $n+360 else $n" | bc; done <tmp0.tmp

The output:

250.628
207.154
121.435
122.107
358.828
241.884

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM