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
3
  • 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) Commented Sep 16, 2013 at 10:22
  • Its definetly an option but there has to be another, cleaner way.
    – Bob
    Commented Sep 16, 2013 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
    Commented Sep 16, 2013 at 10:32

1 Answer 1

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.

3
  • Or sed if it has the -i option.
    – Bobby
    Commented Sep 16, 2013 at 14:33
  • Should probably use mktemp for the temporary file.
    – Daniel Beck
    Commented Sep 18, 2013 at 19:16
  • @Daniel Of course – added that. The OP wanted something "clean" though, so I was hesitant to include even more code when the actual solution is just to use the proper tool, for example.
    – slhck
    Commented Sep 18, 2013 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .