954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sed with arithmetic calculation

Hi everyone,

I am trying to change hundreds of data files using sed command. Text file1 have 10 lines which look like the following
{ x=34280; y...}
{ x=34281; y...}
{ x=34282; y...}
{ x=34283; y...}
{ x=34284; y...}
{ x=34285; y...}
{ x=34286; y...}
{ x=34287; y...}
{ x=34288; y...}
{ x=34289; y...}

What I like to do is to add 10 times j to each of the x values and print it to the same text file. Here is my trial:

#!/bin/bash

for ((  i = 0 ;  i <= 9;  i++  ))
do
j=1
sed -e   's/'3428$i'/`'3428$i'+10*j`/g'  file1 >  file1_new
mv file1_new file1
done


What I am getting is:`{ x=34280+10*j` instead.
any suggestion?

SakuraPink
Light Poster
31 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

How about something along the lines of

sed -e 's/'3428$i'/'$((3428$i + 10*$j))'/g'
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

Great. Thanks a lot for your help.

SakuraPink
Light Poster
31 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You