Technomancy you know you want to

sed -i s/localhost/localhost\ lolcathost/ /etc/hosts

« 2007-10-13T00:00:01Z »

Sandy2007-07-20T04:35:48Z
On Windows
Noah Slater – 2007-07-20T08:17:54Z
This example has two major errors.





Firstly, you need to quote the argument to sed or you will get a "sed: -e expression #1, char 21: unterminated `s' command" error.





Secondly, you cannot use sed to read from a while while concurrently writing to the same file. You need to use a temporary file in this instance.





A working script would look something like:





temporary_file=$(tempfile)


if [ $? -ne 0 ]; then


echo "Could not create temporary file.";


else


sed "s/localhost/localhost lolcathost/" /etc/hosts > $temporary_file;


mv $temporary_file /etc/hosts


fi
Phil2007-07-20T09:54:30Z
Thanks Noah. It was working for me when I piped the output to /etc/hosts2, but shame on me for not testing it all the way through. I also escaped the space, but my blogging software must have munched it.





Perhaps the pithiness of the original could be retained if it were ported to Ruby.
Fnor – 2007-07-20T10:00:06Z
You can also use the -i option of sed to edit in place instead of redirecting to a temporary file : )
Noah Slater – 2007-07-20T10:58:39Z
Aha, but the "-i" option is not portable. Heh.
Noah Slater – 2007-07-20T11:45:25Z
Aha, but the "-i" option is not portable. Heh.
Noah Slater – 2007-07-20T11:46:22Z
Your blogging software should do a redirect after the POST to avoid duplicate submissions by pressing "refresh".
Fnor – 2007-07-20T12:22:32Z
You can also use the -i option of sed to edit in place instead of redirecting to a temporary file : )
Fnor – 2007-07-20T12:25:01Z
Yes, I do agree for the redirect!


And thanks for the portability information.
Phil2007-07-20T17:17:33Z
Noted re: redirects. There's a lot of things this blog software should do. =)





Updated the example; thanks.

Name

URL

HTML will be escaped for now. Comments are currently moderated; sorry.