Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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