Replacing a line in a file using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a line in a file using sed
# 8  
Old 04-15-2012
Quote:
Originally Posted by 47shailesh
's:test1.com:New website is test1.com:g' will not give desired results, as you want to replace test.com with "New website is test1.com" whereas sed will be searcing for "test1.com" which doesn't exist
You are correct, it will not give desired results, and your sed suggestion is correct. However, in the code posted by digitalviking, test1.com will exist. The first expression will change www.test.com leaving www.test1.com which the second expression will find and (incorrectly) change.

After the first expression, the record would have been:
Code:
<html><body><a href="http://www.test1.com">test.com</a></body></html>

With the bold area containing what the second expression would match resulting in the final output for the record:
Code:
<html><body><a href="http://www.New website is test1.com">test.com</a></body></html>

It is important to remember that each expression in a sed programme is applied to each record sequentially (modulo any branching/reading commands).
# 9  
Old 04-15-2012
It works cool!

Last edited by digitalviking; 04-15-2012 at 01:33 PM..
# 10  
Old 04-15-2012
EDIT: Did I really miss something? I thought there was a previous post indicating that the output wasn't going to the file. I'll leave this, but out of context (and of course I didn't hit 'quote') it seems I am just rambling. Odd.

Sed writes to standard output. You can use the -i option (if supported by your version of sed) which edits the file in place. (I recommend using the backup feature of the -i option if you go with this. If your sed doesn't support -i, or you want your script to be portable to systems that don't, then you'll need to do something like this:

Code:
mv input-file input-file.old
if sed '----your sed programme' input-file.old >input-file 
then
  rm input-file.old
else
  mv input-file.old input-file
fi

Which copies your original file to the old copy and runs the sed on that putting the output into the file with the same name. If the sed is successful, the old file is removed. If the sed fails, the original file is restored.

You probably should also test to ensure that the initial move worked before running the sed.
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

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

3. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

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

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

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

7. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

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

9. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: filmguy
4 Replies
Login or Register to Ask a Question