Specifying file to process to Perl one-liner

Documentation on how to launch perl is found in the perlrun man page.

perl -pe'...' -i~ file [file [...]]   # Modifies named file(s) in place with backup.
perl -pe'...' -i file [file [...]]    # Modifies named file(s) in place without backup.
perl -pe'...' file.in >file.out       # Reads from named file(s), outputs to STDOUT.
perl -pe'...' <file.in >file.out      # Reads from STDIN, outputs to STDOUT.

If the file’s name could start with a -, you can use --.

perl -pe'...' [-i[~]] -- "$file" [...]

If you wanted to modify multiple files, you could use any of the following:

find ... -exec               perl -pe'...' -i~ {} +   # GNU find required
find ...         | xargs -r  perl -pe'...' -i~        # Doesn't support newlines in names
find ... -print0 | xargs -r0 perl -pe'...' -i~

In all of the above, square brackets ([]) denote something optional. They should not appear in the actual command. On the other hand, the {} in the -exec clause should appear as-is.


Note: Some one-liners use -n and explicit prints instead of -p. All of the above applies to these as well.

Leave a Comment