Using sed in a for loop | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Using sed in a for loop

UNIX for Dummies Questions & Answers


Tags
for loop, sed - replace text with variable

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-04-2013
plumb_r plumb_r is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 4 February 2013, 3:29 AM EST
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Using sed in a for loop

Hello
I have a group of files

Code:
a1.profile a2.profile a3.profile a4.profile b1.profile b2.profile b3.profile b4.profile

These files all have the same first line with a value s1 atop the columns

Code:
s1_context s1_ref s1_sample s1_% etc

I am trying to use sed in a for loop to replace the s1 in the header of each each file with the initial part of the filename with something like

Code:
for FILE in a1 a2 a3 a4 b1 b2 b3 b4
do
    sed -i.bak "1s/s1/$FILE/g" $FILE.profile > $FILE.profile.header
done

But I am not getting the desired output. I just get $FILE in the header instead.
Thanks
Sponsored Links
    #2  
Old 01-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 3:01 PM EDT
Location: Aachen, Germany
Posts: 4,159
Thanks: 69
Thanked 995 Times in 943 Posts
In bash, using -vx options to log what it's doing, we see exactly the output expected:
Code:
for FILE in a1 a2 a3 a4 b1 b2 b3 b4; do sed "1s/s1/$FILE/g" file; done
+ for FILE in a1 a2 a3 a4 b1 b2 b3 b4
+ sed 1s/s1/a1/g file
a1_context a1_ref a1_sample a1_% etc
+ for FILE in a1 a2 a3 a4 b1 b2 b3 b4
+ sed 1s/s1/a2/g file
a2_context a2_ref a2_sample a2_% etc

so...?
Sponsored Links
    #3  
Old 01-04-2013
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 31 August 2014, 9:55 AM EDT
Location: Vienna, Austria
Posts: 796
Thanks: 18
Thanked 167 Times in 149 Posts
You are using the -i option, which means inplace-update. Therefore the redirection of standard output is obsolete and nothing should be written to the *.header files.

After the loop, the *.profile files hold the contents with the new headers and the *.bak the original contents.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep sed and a loop gunnahafta UNIX for Dummies Questions & Answers 3 05-12-2011 12:21 AM
Using sed in for loop ursaan Shell Programming and Scripting 8 03-31-2010 02:20 AM
For loop with sed sickboy Shell Programming and Scripting 3 09-10-2009 12:54 PM
Loop with sed command to replace line with sed command in it cbo0485 Shell Programming and Scripting 1 08-27-2009 11:19 AM
sed in a for loop marwan UNIX for Dummies Questions & Answers 6 06-30-2007 05:41 PM



All times are GMT -4. The time now is 12:40 PM.