1

I have more than 100 files with with over 9000 lines of text. A preview extract of one of the files looks like this:

<productname>kidscar1</productname>
<productid>98</productid>
<productname>kidscar2</productname>
<productcolor>yellow</productcolor>
<productid>101</productid>
<productname>kidscar3</productname>
<productsize>xxl</productsize>
<productcolor>green</productcolor>
<productid>104</productid>
<productname>kidscar4</productname>
<productcolor>bleu</productcolor>
<productsize>xl</productsize>
<producttype>electric</producttype>
<productid>103</productid>

I'm trying to change (re-arange) the product ID's starting from a different product ID and automatically count this up for the next lines containing an product ID.

I was thinking of a shell script solution which I could use in a for loop.

**sh idscript.sh oldfile.txt 1000 productid > newfile.txt**

Result:

<productname>kidscar1</productname>
<productid>1000</productid>
<productname>kidscar2</productname>
<productcolor>yellow</productcolor>
<productid>1001</productid>
<productname>kidscar3</productname>
<productsize>xxl</productsize>
<productcolor>green</productcolor>
<productid>1002</productid>
<productname>kidscar4</productname>
<productcolor>bleu</productcolor>
<productsize>xl</productsize>
<producttype>electric</producttype>
<productid>1003</productid>`

I know that it's possible to replace a whole line in sed with the next command:

**sed "s/<productid>100</productid>=.*/<productid>=<productid>1000</productid>/g"**

But how can I make this work to get the above result? If there are other (simpler) ways to achieve this, i like to hear it also!

2 Answers 2

2

sed is for s/old/new that is all. In this case you need a numeric variable to keep track of the incrementing product and sed doesn't support variables at all so just use awk:

$ awk -v pid=1000 '/<productid>/{sub(/[0-9]+/,pid++)}1' file
<productname>kidscar1</productname>
<productid>1000</productid>
<productname>kidscar2</productname>
<productcolor>yellow</productcolor>
<productid>1001</productid>
<productname>kidscar3</productname>
<productsize>xxl</productsize>
<productcolor>green</productcolor>
<productid>1002</productid>
<productname>kidscar4</productname>
<productcolor>bleu</productcolor>
<productsize>xl</productsize>
<producttype>electric</producttype>
<productid>1003</productid>
Sign up to request clarification or add additional context in comments.

3 Comments

Nice. productid string is used as RS and removing all numbers and writing the incremented pid startig from 1000?
No, pid is a variable containing a number and I'm just replacing the existing number with its value and post-incrementing it. I'm not doing anything with RS.
Thanx for the infor Ed, exactly what I was looking for!
0

Following awk may also help you on same too.

awk -v val=1000 '/productid/{sub(/>[0-9]+</,">"val++"<")} 1'  Input_file

1 Comment

@EdMorton, done the change sir, yes I thought if in case null value there for <productid></productid>, though OP didn't mention it but thought to cover it in it, thanks for letting me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.