Index

Table of contents

perl

hello world
#!/usr/bin/perl
print "hello";
enable warnings for file if run by default interpreter
#!/usr/bin/perl -w
run script
perl [file]
enable warnings for file and all of its includes
perl -w [file]
enable warnings (verbose) for file and all of its includes
perl -Mdiagnostics [file]
lookup atan2 in perl documentation
perldoc -u -f atan2

flags

execute inlined code
perl -e 'print "hello world\n"';
execute inline code with features of latest perl version
perl -E 'say "hello world"'
split input on whitespace and place in array @F
[cmd] | perl -a
perl -a [file]
split input on custom separator and place in array @F
perl -F','
remove newlines from input and add newlines to output
[cmd] | perl -l
perl -l [file]
process script for every line in input
[cmd] | perl -n
perl -n [file]
process input and autoprint line ($_)
[cmd] | perl -p
perl -p [file]
edit file inplace
perl -i [file]
create backup with extension .bak
perl -i.bak [file]

examples

print input unchanged
ps ax | perl -pe ''
ps ax | perl -ne 'print'
add line numbers
ps ax | perl -pe '$_="$.) $_"'
ps ax | perl -ne 'print "$.) $_"'
print java processes
ps ax | perl -ne 'print if /java/'
regex replace
ps ax | perl -pe 's/java/python/'
print first column only (in this example: process id)
ps ax | perl -lae 'print $F[0]';
ps ax | perl -ne 's/^ *//;s/ .*//; print'
edit file (sed immitation)
perl -ipe 's/foo/bar/' file.txt
edit file with backup
perl -i.orig -pe 's/foo/bar/' file.txt
print first column in comma separated csv
perl -F',' -E 'say "$F[0]"' comma.csv
documentation for command line invocation
https://perldoc.perl.org/perlrun.html

documentation

https://perldoc.perl.org