I'm using sed to find configuration directives in a php file. Essentially, I need to set the strings database_name_here, username_here and password_here with their appropriate values in the config-sample.php (and rename the file to config.php). My current solution uses sed three times, redirecting the output to temporary files.

sed -e 's/database_name_here/foo/g' config-sample.php > /tmp/config.1
sed -e 's/username_here/bar/g' /tmp/config.1 > /tmp/config.2
sed -e 's/password_here/bat/g' /tmp/config.2 > config.php

I was wondering how I could achieve the same results without having to create two temporary files?

link|improve this question
feedback

1 Answer

up vote 4 down vote accepted

You can pass multiple -e arguments to sed. On each line, each transformation is applied in turn.

<config-sample.php sed -e 's/database_name_here/foo/g' -e 's/username_here/bar/g' -e 's/password_here/bat/g' >config.php

Some implementations also let you separate multiple commands with a ;, but that's not always the case, and doesn't work with commands that expect a terminating newline. Most (but still not all) implementations let you separate multiple commands with a newline.

Note that if you had needed multiple commands, you wouldn't have needed to use temporary files, you could have used pipes.

<config-sample.php sed -e '…' | grep -v '^#' >config-without-comments.php
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.