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
sed -i 'humans' /list.txt

Any help please...

sed s:people:humans:g list.txt

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>

sed -e 's:www.test.com:www.test1.com:g' -e 's:test.com:New website is test1.com:g' infile
1 Like
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

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

'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:

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'

thanks that works great even with variable in!

Cheers

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:

<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:

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

It works cool!

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:

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.