Sed replacing a whole line with a new line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed replacing a whole line with a new line
# 1  
Old 01-18-2008
Error Sed replacing a whole line with a new line

I am needing to replace a whole line of code in a file with a new line of code. This is what I need to do.

I need to replace.

$db_url = 'mysql://test_dev:test12@localhost/test';

With

$db_url = 'mysql://$dbuser:$dbpass@localhost/$dbase';

OK this is where it gets complicated. The first $db_url = line is not allways going to be set to test_dev:test12 etc... I need to be able to find the $db_url line and replace it with the second line. I hope this makes sence. Its just alot safer if I can replace the whole line.

Yes the $dbuser, $dbpass, $dbase are variables I am using in my script.

So any help would be appretiated.

filmguy

SmilieSmilieSmilie
# 2  
Old 01-18-2008
You can try this, it searches for a line with "$db_url =" to do the replacement, the trick is to escape the single quotes correctly:

Code:
sed 's!\$db_url =.*!$db_url = '"'"'mysql://$dbuser:$dbpass@localhost/$dbase'"'"';!' file

Regards
# 3  
Old 01-18-2008
Escaping quotes??

OK so how do I do that??

Code:
sed 's!\$db_url =.*!$db_url = '"'"'mysql://$dbuser:$dbpass@localhost/$dbase'"'"';!' file


I know nothing about sed really so how do I get the above statement to work in redhad es 4 server ?? When I tried it all it did was print the file to the screen. I replaced file with my file name and it printed the file to the screen. No replacement was done in the file.

Filmguy
# 4  
Old 01-19-2008
Redirect the output to another file.
Then do diff to see the difference.
# 5  
Old 01-19-2008
sed prints the result to the screen (standard output).
You can redirect the output to a file and replace the original file with it:

Code:
sed 's!\$db_url =.*!$db_url = '"'"'mysql://$dbuser:$dbpass@localhost/$dbase'"'"';!' orig_file > new_file

If the replacements are correct, replace the original file with the new file:

Code:
mv new_file orig_file

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. 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

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. 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

5. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

6. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

7. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

8. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

9. Shell Programming and Scripting

Finding and replacing whole line using sed

Hi all, assume that i am having the following line in a file called file1. triumph and disaster must be treated same. I want to replace this line with. follow excellence success will chase you. is it possible to do this using sed. if possible kindly post me the... (2 Replies)
Discussion started by: anishkumarv
2 Replies

10. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies
Login or Register to Ask a Question