LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Changing sed in loop (https://www.linuxquestions.org/questions/linux-newbie-8/changing-sed-in-loop-863991/)

rickard.solsjo 02-21-2011 03:35 AM

Changing sed in loop
 
Hi
I am trying to find out how to change the value of the inputs in sed while looping
As such:

for i in `seq 1 10`; do

"do something funny, using OpenFOAM"

cd system
sed 's/oldvalue/oldValue+increment/' -i sampleDict
cd ..
done

So basically, can you for instance create a constant "a" and increase it for every loop interval and use it for sed?

Appreciate the help, n00b-warning.

druuna 02-21-2011 03:43 AM

Hi,

Is this what you are looking for:
Code:

#!/bin/bash

oldvalue=1

for i in $(seq 1 10)
do
  let newvalue=oldvalue+$i
  echo sed -i "s/$oldvalue/$newvalue/" sampleDict
done

You need to use double quotes with sed, then you can use variables inside the statement.

Hope this helps.

Remove the echo in front of the sed statement if this is to your liking.

catkin 02-21-2011 03:46 AM

Not knowing what oldvalue or the increment are, not even knowing if they are numbers, it's hard to answer well but something like this:
Code:

oldValue=5
for i in `seq 1 10`
do
    <do something funny, using OpenFOAM>
    sed "s/$oldvalue/$(( oldValue + i))/" -i system/sampleDict
done



All times are GMT -5. The time now is 11:51 PM.