2

If want to filter a file but if I use the same file as input and output the output file is empty. Is there another solution instead of using a second file?

php -r "echo preg_replace('~/\* DEV \*/(.*)/\* ENDDEV \*/~si', '', file_get_contents('php://stdin'));" < build/index.php > build/index.php
Bob
  • 23
  • 2
  • use the file as a basis, then create a new one, delete the original and rename the new one is not an option i assume. this could also be useful as a backup basis, since you're editing a sensitive file (an index) – Lorenzo Von Matterhorn Sep 16 '13 at 10:22
  • Its definetly an option but there has to be another, cleaner way. – Bob Sep 16 '13 at 10:23
  • 2
    I'm fairly sure that there actually isn't a cleaner way, since even programs with an 'edit in place' option, like `sed`, *actually* create temporary files and then overwrite. – evilsoup Sep 16 '13 at 10:32

1 Answers1

6

When you open a file for writing with >, it is truncated before you even read it. That's why it's empty.

Alternative with a temporary file (yes, this is quite common):

php -r "…" < build/index.php > tmp
mv tmp build/index.php

Ideally, use mktemp to generate a unique temporary file:

tmp="$(mktemp /tmp/file.XXX)"
php -r "…" < build/index.php > "$tmp"
mv "$tmp" build/index.php

But this isn't really shorter or more concise.


My personal favorite is the sponge utility from moreutils – it reads input and then writes to the file:

php -r "…" < build/index.php | sponge build/index.php

Or, better yet, don't do the substitution in PHP, but a tool that supports in-place editing, like Perl with:

perl -pi -e '…' build/index.php

Similar with sed, here the in-place flag for GNU sed:

sed -i -e '…' build/index.php

And for BSD sed, you need an explicit empty argument for -i:

sed -i '' -e '…' build/index.php

You will probably have to adapt your regular expression / substitution command for Perl's and sed's syntax, respectively.

slhck
  • 223,558
  • 70
  • 607
  • 592