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
# 1  
Old 04-15-2012
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....

Code:
if cat /list.txt | grep -q 'people' ; then
sed -i 'humans' /list.txt

Any help please...
# 2  
Old 04-15-2012
Code:
sed s:people:humans:g list.txt

# 3  
Old 04-15-2012
what if the the line was

<html><body><a href="http://www.test.com">test.com</a></body></html>

and I wanted to change it too


<html><body><a href="http://www.test1.com">New website is test1.com</a></body></html>
# 4  
Old 04-15-2012
Code:
sed -e 's:www.test.com:www.test1.com:g' -e 's:test.com:New website is test1.com:g' infile

This User Gave Thanks to 47shailesh For This Post:
# 5  
Old 04-15-2012
Code:
sed -e 's:www.test.com:www.test1.com:g' -e 's:test.com:New website is test1.com:g' infile

Is that right surly it should be???? Will it still work? I changed s:test.com to s:test1.com and infile to list.txt

Code:
sed -e 's:www.test.com:www.test1.com:g' -e 's:test1.com:New website is test1.com:g' list.txt

# 6  
Old 04-15-2012
'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

---------- Post updated at 07:34 AM ---------- Previous update was at 07:31 AM ----------

play with following code, it will make working more clear to you:
Code:
echo "<html><body><a href="http://www.test.com">test.com</a></body></html>" |  sed -e 's:www.test.com:www.test1.com:g' -e 's:test.com:New website is test1.com:g'

# 7  
Old 04-15-2012
thanks that works great even with variable in!

Cheers
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

Featured Tech Videos