我需要从浮点数转换为十进制的csv文件的第7列。这是一个巨大的文件,我不想使用while read转换。awk有什么快捷方式吗?
输入:
"xx","x","xxxxxx","xxx","xx","xx"," 00000001.0000"
"xx","x","xxxxxx","xxx","xx","xx"," 00000002.0000"
"xx","x","xxxxxx","xxx","xx","xx"," 00000005.0000"
"xx","x","xxxxxx","xxx","xx","xx"," 00000011.0000"
输出:
"xx","x","xxxxxx","xxx","xx","xx","1"
"xx","x","xxxxxx","xxx","xx","xx","2"
"xx","x","xxxxxx","xxx","xx","xx","5"
"xx","x","xxxxxx","xxx","xx","xx","11"
尝试这些,工作。还有更简单的吗?
awk 'BEGIN {FS=OFS="",""} {$7 = sprintf("%.0f", $7)} 1' $test > $test1
awk '{printf("%s"n", $0)}' $test1
使用您的示例,请尝试以下awk
程序。
awk -v s1=""" -v OFS="," '{$NF = s1 ($NF + 0) s1} 1' Input_file
解释:简单的解释是,在主程序中将OFS
设置为,
;在每行的最后一个字段中只保留数字并用"
覆盖最后一个字段,重新洗牌字段并打印编辑/未编辑的所有行。
另一个简单的awk
解决方案:
awk 'BEGIN {FS=OFS="",""} {$NF = $NF+0 """} 1' file
"xx","x","xxxxxx","xxx","xx","xx","1"
"xx","x","xxxxxx","xxx","xx","xx","2"
"xx","x","xxxxxx","xxx","xx","xx","5"
"xx","x","xxxxxx","xxx","xx","xx","11"
awk 'BEGIN{FS=OFS=","} {gsub(/"/, "", $7); $7=""" $7+0 """; print}' file
输出:
<>之前"xx","x","xxxxxx"、"xxx"、"xx"、"xx","1"xx"、"x","xxxxxx"、"xxx"、"xx"、"xx","2"xx"、"x","xxxxxx"、"xxx"、"xx"、"xx","5"xx"、"x","xxxxxx"、"xxx"、"xx"、"xx","十一"
gsub(/"/, "", $7)
:删除所有从7美元"
$7+0
:将$7中的数字减少到最小表示