Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed increment number

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>
like image 370
Luis Avatar asked Dec 16 '10 16:12

Luis


People also ask

How do I increment a number using SED?

To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception: when the digit is a nine the previous digits must be also incremented until you don't have a nine.

What is I flag in SED?

The I flag allows to match a pattern case insensitively. Usually i is used for such purposes, grep -i for example. But i is a command (discussed in append, change, insert chapter) in sed , so /REGEXP/i cannot be used. The substitute command does allow both i and I to be used, but I is recommended for consistency.


1 Answers

while read line; do n=$((++n)) &&  echo $line|sed -e 's/[0-9]/'$(($n))'/' ; done < patt
like image 198
Someguy Avatar answered Sep 19 '22 15:09

Someguy