I'd like to substitute some text with an incremental value. Considering file xx:
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
and sed command:
for n in seq 3;do sed -e 's/<RecordID>\d/<RecordID>'`echo $n`'/' xx; done
the echo $n
command does not get incremented.
Tryed also:
n=1; sed -e 's/<RecordID>/<RecordID>'`echo $n ;let n=$n+1`'/g' xx
but with no success.
Considering only sed (no awk or perl) how can I have the RecordID field incremented as in:
<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>2</RecordID>
<outro>dfdfd</outro>
<RecordID>3</RecordID>
<outro>dfdfd</outro>
sed
because you have to do the incrementing outside of it in a shell and then call one instance of sed for each line to process. If this is just for academic purposes, fine, but you really aught to be usingawk
for this because it can do math nativity. In fact the entire awk script is this one short line:awk '/RecordID/{sub("[0-9]+",++i)}1' infile > outfile
– Beckford