LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2011, 03:35 AM   #1
rickard.solsjo
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
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.
 
Old 02-21-2011, 03:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
 
Old 02-21-2011, 03:46 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed in a bash loop with ascending output gary_in_springhill Programming 20 01-14-2011 10:35 AM
Problem changing line with sed. Should i use AWK? RattleSn@ke Linux - General 7 11-12-2007 01:03 PM
Changing multiple files with SED timmy01 Linux - Software 17 05-15-2007 08:10 AM
changing case in sed phoenix7 Linux - Software 8 08-30-2005 07:18 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration