Quick text transformation using Perl one-liner
Perl, as one programming language, is not well designed, but it could be handy for simple text transformation.
§Command Options
The most common form of using Perl, for me, is one-liner, and it usually goes like this:
1 | perl -i -pe 's!orignal!new!' <file> |
Let decipher the cryptic options:
- -p: do the transformation for matched patterns or print if unmatched
1 | while (<>) { |
-
-i: in-place modification instead of writing to STDOUT
-
-e: evaluate CODE; where we specify the one-liner script
§Match multiple lines
1 | perl -i -pe 'BEGIN { undef $/ }; s!orignal!new!s' <file> |
Firstly, unset the variable of $/
so that the whole file is slurped into memory, instead of being read line by line. Secondly, append s
option to
the substitute code so that .
could match newline.