PatricF
PatricF

Reputation: 419

Perl: edit file, not just output to shell

I found a little one-liner of perl code that will change the serial in my zone-files on my Bind server. However it wont change the actual file, it just gives me the output directly to the shell.

This is what I run:

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

This gives me the correct output to the shell and if I remove the print; at the end of the perl line nothing happens and I want it to actually change the files to the output I got.

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

Upvotes: 3

Views: 149

Answers (1)

tivn
tivn

Reputation: 1923

I am assuming you want to replace the string inside the files found by find. Command example below will change in-place (-i) any "foo" with "bar" for all *.txt files from curent 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 good habit to always use single quotes rather than double quotes. This is because inside double quotes, a \, $, etc. may be processed by the shell before passed to Perl. See Bash manual.

Upvotes: 3

Related Questions