我想用增量值代替一些文本。
考虑文件xx:

<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>

和sed命令:
for n in seq 3;do sed -e 's/<RecordID>\d/<RecordID>'`echo $n`'/' xx; done
echo $n命令不会增加。

也尝试过:
n=1; sed -e 's/<RecordID>/<RecordID>'`echo $n ;let n=$n+1`'/g' xx

但没有成功。

仅考虑sed(没有awk或perl),如何使RecordID字段递增,如下所示:
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>2</RecordID>
<outro>dfdfd</outro>
<RecordID>3</RecordID>
<outro>dfdfd</outro>

最佳答案

while read line; do n=$((++n)) &&  echo $line|sed -e 's/[0-9]/'$(($n))'/' ; done < patt

关于variables - sed增量编号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4462954/

10-08 22:11