Perl one-liners


  • Read line, substitute regex and print line.
    • perl  -ne '{$l = $_; $l=~ s/dede/frfr/g; print $l;}' input1.txt
    • perl –ne ‘$l = $l =~ s/dede/frfr/g; print $l;}’ input1.txt input2.txt
    • -e means “execute” and –n makes it loop line by line
    • Multiple input files are fine.
  • Snazzier replacement per line

    • perl  -pe 's/a/b/g' < input > newfile
  • Read line and print the (1-indexed) line number and line.
    • perl –ne ‘print “$. – $_”’ input1.txt
    • output looks like:
      • 1 – My first line of input text
      • 2 – My second line of input text
  • With no input, print 0 to 999
    • perl -e ' for ($i=0;$i<999;$i++) { print "$i\n"}  '
THESE DON'T WORK WELL WITH sed
perl -pe "s/\&apos/'/g" < 2.2_cleaner > new
perl -pe 's/\&quot/"/g' < 2.2_cleaner > new