3

我正在尝试用逗号分隔的 13 个字段替换字符串中的字段。我既有要替换的位置,也有要在分隔变量中赋予该位置的新值。使用 bash。

#Original string:
THR=’0,0,0,0,0,0,0,0,0,0,0,0,0’
#Position I want to manipulate:
pos=’3’
#Value I want to write in the field "pos"
val=’8.73’

# What I'm trying to do:
echo $THR | awk -v chawk=$pos -v thrawk=$val -F',' '{ print; $chawk=$thrawk; print }'

THR 应该是 '0,0,8.73,0,0,0,0,0,0,0,0,0,0'

干杯

4

1 回答 1

2

你已经差不多了,去掉$前面$thrawk

awk -v chawk=$pos -v thrawk=$val -F',' '{ print; $chawk=thrawk; print }' OFS=\. <<< $THR
0,0,0,0,0,0,0,0,0,0,0,0,0
0.0.8.73.0.0.0.0.0.0.0.0.0.0

将其恢复到变量 THR

THR=0,0,0,0,0,0,0,0,0,0,0,0,0
THR=$(awk -v chawk=$pos -v thrawk=$val -F',' '{ $chawk=thrawk; print }' OFS=\. <<< $THR)
echo $THR
0.0.8.73.0.0.0.0.0.0.0.0.0.0
于 2013-10-25T05:33:46.563 回答