sed to replace a line with modified line in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to replace a line with modified line in same file
# 1  
Old 11-29-2011
sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..?
i have come up till the below code
Code:
 sed "s/$line1/$line2/g" tmpfile.sql

the code replaces fine but its not replaced in same file..it just displays it, i could direct to another temporary file but i wast the replacement to be in same file... plz help..
# 2  
Old 11-29-2011
sed -i, or use a temporary file and move it afterwards.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 11-29-2011
cat input:
Code:
hello
world

Code:
sed -i 's/world/earth/g' input

cat input:
Code:
hello
earth

# 4  
Old 11-29-2011
thanks it worked :-)

---------- Post updated at 05:56 PM ---------- Previous update was at 05:39 PM ----------

oh am sorry it dint work....
here what i tried...
contents of tmpfile.sql is
Code:
CREATE TABLE `ZoneVoiceRecorderVoiceRecordersMap` (
  `zoneId` int(11) DEFAULT NULL,
  `voiceRecordersId` int(11) DEFAULT NULL,
  `lastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `zoneId` (`zoneId`,`voiceRecordersId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

the code which i used is
Code:
while read line1
do
 if [[ `expr match "$line1" ".*DEFAULT NULL.*"` != "0" ]]; then
                line2="blah blah blah"
                sed -i 's/$line1/$line2/g' tmpfile.sql
        fi
done < tmpfile.sql

i am not getting any error but when i open tmpfile.sql its not changed....
# 5  
Old 11-29-2011
Use double quotes with sed if you have variables inside...
Code:
sed -i "s/$line1/$line2/g" tmpfile.sql

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 11-29-2011
Oh thanks.. its working now... :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed replace to rename each line a file

Have a file in this format This is line one ; line_one This is line two ; line_two This is line three ; line_three This is line four ; line four. I'm trying to make each line a new file called line_one line_two line_three line_four. Tried using split -1 but then I'm back needing to rename... (3 Replies)
Discussion started by: jimmyf
3 Replies

2. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

7. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

8. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

9. Shell Programming and Scripting

Replace a line in a file with sed

Hi, I am trying to write a script that will replace "PermitEmptyPasswords yes" with "PermitEmptyPasswords no". The following does not seem to work: - sed 's!/"PermitEmptyPasswords yes"/!/"PermitEmptyPasswords no"/!' Appreciate any ideas. Thanks (2 Replies)
Discussion started by: mtech3
2 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question