簡體   English   中英

使用awk將整數加和到bash中的列

[英]Using awk to sum an integer to a column in bash

我有一個帶有負值和正值的一列文件(tmp0.tmp),如下所示:

-109.372
-152.846
121.435
122.107
-1.172
-118.116

我想對每個負值求和360,並將正值保持在同一位置。

我做了:

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

而且沒有用

誰能幫我嗎?

謝謝

您可以使用awk ,無需使用loops

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

bash + bc另一種方式:

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

輸出:

250.628
207.154
121.435
122.107
358.828
241.884

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM