The number of increments sed

I would like to substitute some text with an extra value. Given the xx file:

<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 command echo $ndoes not receive an increment.

Tried also:

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

but without success.

Given only sed (no awk or perl), how can I increase the RecordID field, as in:

<outro>dfdfd</outro>
<RecordID>1</RecordID>
<outro>dfdfd</outro>
<RecordID>2</RecordID>
<outro>dfdfd</outro>
<RecordID>3</RecordID>
<outro>dfdfd</outro>
+5
source share
4 answers
while read line; do n=$((++n)) &&  echo $line|sed -e 's/[0-9]/'$(($n))'/' ; done < patt
+5
source

Despite the statement in the question that Perl and Awk cannot be used, the answers presented so far indicate that sedit is not the right tool for this task. Both answers that I see only work with an extremely limited data set and only work with extremely small data sets.

sed, . , , (: 3 4, 2 3, 3 4).

Perl Python, .

perl -e 's{<RecordID>(\d+)</RecordID>}{$n=$n+1; "<RecordID>$n</RecordID>"}e'

: " ". sed - , ( , ). .

+4

-, sed \d. [0-9] [[:digit:]].

-, for n in seq 3 n "seq" "3". for n in $(seq 3) ( Bash) for n in {1..3}.

-, . sed -i ( ), sed ... xx > xx.tmp && mv xx.tmp xx.

-, , .

, , :

for n in $(seq 3); do sed -e $(($n*2))'s/<RecordID>[[:digit:]]/<RecordID>'$n'/' xx > xx.tmp && mv xx.tmp xx; done
+2

sed - , , . - , , script sed. , . , , - .

0
source

All Articles