我有以下CSV文件:
2012-07-12 15:30:09; 353.2
2012-07-12 15:45:08; 347.4
2012-07-12 16:00:08; 197.6
2012-07-12 16:15:08; 308.2
2012-07-12 16:30:09; 352.6
我想要做的是修改第二栏中的值...
我已经可以做的是提取值并以这种方式修改它:
#!/bin/bash
cut -d ";" -f2 $1 > .tmp.csv
for num in $(cat .tmp.csv)
do
(echo "scale=2;$num/5" | bc -l >> .tmp2.csv)
done
rm .tmp.csv
rm .tmp2.csv
但我也需要在该文件中使用column1 ......
我希望你们中的一个能给我一个提示,我只是被卡住了!
答案 0 :(得分:3)
从您的代码中,这是我理解的
<强>输入强>
2012-07-12 15:30:09; 353.2
2012-07-12 15:45:08; 347.4
2012-07-12 16:00:08; 197.6
2012-07-12 16:15:08; 308.2
2012-07-12 16:30:09; 352.6
Awk代码
awk -F“;” '{print $ 1“;” $ 2/5}'输入
<强>输出强>
2012-07-12 15:30:09;70.64
2012-07-12 15:45:08;69.48
2012-07-12 16:00:08;39.52
2012-07-12 16:15:08;61.64
2012-07-12 16:30:09;70.52
答案 1 :(得分:3)
单向,使用awk
:
awk '{ $NF = $NF/5 }1' file.txt
结果:
2012-07-12 15:30:09; 70.64
2012-07-12 15:45:08; 69.48
2012-07-12 16:00:08; 39.52
2012-07-12 16:15:08; 61.64
2012-07-12 16:30:09; 70.52
HTH
答案 2 :(得分:2)
尝试awk
:
awk '
BEGIN {
## Split fields with ";".
FS = OFS = "; "
}
{
$2 = sprintf( "%.2f", $2/5 )
print $0
}
' infile
输出:
2012-07-12 15:30:09; 70.64
2012-07-12 15:45:08; 69.48
2012-07-12 16:00:08; 39.52
2012-07-12 16:15:08; 61.64
2012-07-12 16:30:09; 70.52
答案 3 :(得分:2)
这是一个几乎纯粹的bash
解决方案,没有临时文件:
#!/bin/bash
while IFS=$';' read col1 col2; do
echo "$col1; $(echo "scale=2;$col2/5" | bc -l)"
done