Perl: edit a file, not just shell out

I found some one line perl code that will change the serial number in my zone files on my Bind server. However, it won't change the actual file, it just gives me the output directly to the shell.

This is what I am running:

find . -type f -print0 | xargs -0 perl -e "while(<>){ s/\d+(\s*;\s*[sS]erial)/2015050466\1/; print; }"

      

This gives me correct shell output, and if I delete print;

at the end of the perl line, nothing happens and I want it to actually change the files to the output it produces .

I'm still noob when it comes to Perl, so this might be a simple fix, so any answer would be appreciated.

+3


source to share


1 answer


I am assuming that you want to replace the string inside the files found with find

. The example below will change in place ( -i

) any "foo"

to "bar"

for all files *.txt

from the current directory.

find . -type f -name '*.txt' -print0 | xargs -0 perl -p -i -e 's/foo/bar/g;'

      

And for your question, you should be able to get it with this command:



find . -type f -print0 | xargs -0 perl -p -i -e 's/\d+(\s*;\s*[sS]erial)/2015050466\1/;'

      

Note. It is a good habit to always use single quotes, not double quotes. This is due to the fact that the internal double quotes \

, $

etc., can be processed prior to transmission to shell Perl. See the Bash manual .

+3


source







All Articles