|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
doneBut I am not getting the desired output. I just get $FILE in the header instead. Thanks |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| for loop, sed - replace text with variable |
| Thread Tools | Search this Thread |
| 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 |
|
|