One-Liners 101 Advanced Perl One-Liners Summary Examples

Essential One-Liners
Walter C. Mankowski
Department of Computer Science Drexel University Philadelphia, PA

YAPC::Chicago June 16, 2008

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Why One-Liners?

Perl’s got a reputation for producing unreadable, unmaintainable code.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Why One-Liners?

Perl’s got a reputation for producing unreadable, unmaintainable code. A lot of work has been done on tools and techniques to get around that.

Walt Mankowski

Essential One-Liners

Walt Mankowski Essential One-Liners . A lot of work has been done on tools and techniques to get around that. Perl’s still great for throwing together quick and dirty little programs. unmaintainable code.One-Liners 101 Advanced Perl One-Liners Summary Examples Why One-Liners? Perl’s got a reputation for producing unreadable.

One-Liners 101 Advanced Perl One-Liners Summary Examples Why One-Liners? Perl’s got a reputation for producing unreadable. unmaintainable code. Walt Mankowski Essential One-Liners . Nothing’s quicker and dirtier than the one-liner. Perl’s still great for throwing together quick and dirty little programs. A lot of work has been done on tools and techniques to get around that.

tr. sed. xargs. etc. Walt Mankowski Essential One-Liners . sort. cut. Perl fits really well into this niche. wc. simple existing programs in pipelines less.One-Liners 101 Advanced Perl One-Liners Summary Examples But I’m a Java programmer! Text files are everywhere. tee. The Unix command-line environment is incredibly powerful: everything is ASCII create new “programs” by connecting small.

One-Liners 101 Advanced Perl One-Liners Summary Examples Freeing your time for more important things . . Walt Mankowski Essential One-Liners . .

world Newlines Loops Perl One-Liners 101 Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

world Newlines Loops Perl One-Liners 101 (Cell phones off?) Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

\n"’ Walt Mankowski Essential One-Liners . world Newlines Loops Hello.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world. world % perl -e ’print "Hello.

world % perl -e ’print "Hello.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. % Walt Mankowski Essential One-Liners . world Newlines Loops Hello. world.\n"’ Hello. world.

% -e to enter the program on the command line Walt Mankowski Essential One-Liners .\n"’ Hello.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world % perl -e ’print "Hello. world Newlines Loops Hello. world. world.

% -e to enter the program on the command line enclose program in single quotes to avoid shell expansion Walt Mankowski Essential One-Liners . world. world.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.\n"’ Hello. world Newlines Loops Hello. world % perl -e ’print "Hello.

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world % perl -e ’print "Hello. world Newlines Loops Hello.\n"’ Hello. world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you don’t need the final semicolon Walt Mankowski Essential One-Liners . world.

% -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you don’t need the final semicolon no strict Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world % perl -e ’print "Hello.\n"’ Hello. world. world. world Newlines Loops Hello.

\n"’ Hello. world % perl -e ’print "Hello. world.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world Newlines Loops Hello. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you don’t need the final semicolon no strict no warnings Walt Mankowski Essential One-Liners . world.

world.\n"’ Hello.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world % perl -e ’print "Hello. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you don’t need the final semicolon no strict no warnings no tests Walt Mankowski Essential One-Liners . world. world Newlines Loops Hello.

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. Walt Mankowski Essential One-Liners . world Newlines Loops That’s it! That’s all you need to know.

The rest of this talk is all about syntactic sugar to make one-liners easier to write.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world Newlines Loops That’s it! That’s all you need to know. Walt Mankowski Essential One-Liners .

world Newlines Loops Perl programmers love syntactic sugar. Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.\n"’ Walt Mankowski Essential One-Liners . Without -l perl -e ’print "Hello. world. world Newlines Loops Automatic newlines with -l The -l flag automatically adds a newline to whatever you print.

world Newlines Loops Automatic newlines with -l The -l flag automatically adds a newline to whatever you print.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello."’ Walt Mankowski Essential One-Liners . Without -l perl -e ’print "Hello.\n"’ With -l perl -le ’print "Hello. world. world.

say.10 introduced a new builtin function. that works just like print except that it automatically adds a newline. world Newlines Loops say what? Perl 5. Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

say."" Execution of -e aborted due to compilation errors.10 introduced a new builtin function. near "say "Hello. world Newlines Loops say what? Perl 5. that works just like print except that it automatically adds a newline."’ String found where operator expected at -e line 1. % Walt Mankowski Essential One-Liners . world. world. Sadly. world.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. near "say "Hello. say doesn’t work with -e: % perl -e ’say "Hello."" (Do you need to predeclare say?) syntax error at -e line 1.

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world Newlines Loops say what? (continued) To avoid breaking backward compatibility. Walt Mankowski Essential One-Liners .10. say is turned off by default in 5.

"’ Hello. use -E instead of -e: % perl -E ’say "Hello. To turn it on from the command line. say is turned off by default in 5.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world. % Walt Mankowski Essential One-Liners .10. world. world Newlines Loops say what? (continued) To avoid breaking backward compatibility.

pl Walt Mankowski Essential One-Liners . Here’s one way to write that: perl -e ’while (<>) {print if length > 80}’ *. world Newlines Loops Writing Loops Suppose you want to see if your team is following your new coding standards that lines can’t be longer than 80 characters.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. so perl has a -n flag that automatically puts a loop around your code.. # your code goes here } Walt Mankowski Essential One-Liners . It’s equivalent to while {<>} { . world Newlines Loops The -n flag That gets tedious to write that all the time..

.pl Walt Mankowski Essential One-Liners . It’s equivalent to while {<>} { . world Newlines Loops The -n flag That gets tedious to write that all the time. # your code goes here } Without -n perl -e ’while (<>) {print if length > 80}’ *. so perl has a -n flag that automatically puts a loop around your code..One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

so perl has a -n flag that automatically puts a loop around your code.pl Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.. # your code goes here } Without -n perl -e ’while (<>) {print if length > 80}’ *. It’s equivalent to while {<>} { . world Newlines Loops The -n flag That gets tedious to write that all the time.pl With -n perl -ne ’print if length > 80’ *..

One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.or post-processing when using the -n flag. For example. world Newlines Loops BEGIN and END blocks If you want to do pre. if the file nums contains 1 2 3 4 Walt Mankowski Essential One-Liners . use BEGIN and END blocks.

world Newlines Loops BEGIN and END blocks (continued) Sum % perl -lne ’$s += $_.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. END{print $s}’ nums 10 % Walt Mankowski Essential One-Liners .

END{print $s}’ nums 10 % Product % perl -lne ’BEGIN{$p = 1} $p *= $_. END{print $p}’ \ nums 24 % Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world Newlines Loops BEGIN and END blocks (continued) Sum % perl -lne ’$s += $_.

print’ foo Walt Mankowski Essential One-Liners . world Newlines Loops Writing Loops (continued) Suppose you want to convert an existing file to lowercase.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. Now that you know about the -n flag. you might try writing it like this: perl -ne ’tr/A-Z/a-z/.

print’ foo perl -ne ’tr/A-Z/a-z/. Now that you know about the -n flag.out Walt Mankowski Essential One-Liners . world Newlines Loops Writing Loops (continued) Suppose you want to convert an existing file to lowercase. you might try writing it like this: perl -ne ’tr/A-Z/a-z/. print’ foo >foo.One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

the -p flag.. world Newlines Loops The -p flag Printing each line is a common enough operation that perl has a special flag for it. # your code goes here } continue { print or die "-p destination: $!\n".. } Walt Mankowski Essential One-Liners . It’s equivalent to while {<>} { .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello.

. # your code goes here } continue { print or die "-p destination: $!\n".One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. } It’s similar to the -n flag. world Newlines Loops The -p flag Printing each line is a common enough operation that perl has a special flag for it. print’ foo Walt Mankowski Essential One-Liners . the -p flag.. It’s equivalent to while {<>} { . except -p prints out each line: Without -p perl -ne ’tr/A-Z/a-z/.

. It’s equivalent to while {<>} { . except -p prints out each line: Without -p perl -ne ’tr/A-Z/a-z/. # your code goes here } continue { print or die "-p destination: $!\n". print’ foo With -p perl -pe ’tr/A-Z/a-z/’ foo Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Hello. world Newlines Loops The -p flag Printing each line is a common enough operation that perl has a special flag for it.. the -p flag. } It’s similar to the -n flag.

One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Advanced Perl One-Liners Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Editing files in-place Instead of just printing out the file. Perl’s -i flag does exactly that: Walt Mankowski Essential One-Liners . one thing you might want to do with the -p flag is make the changes directly to the file.

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing files in-place Autosplit Using modules Input record separators

Editing files in-place
Instead of just printing out the file, one thing you might want to do with the -p flag is make the changes directly to the file. Perl’s -i flag does exactly that: Print to stdout perl -pe ’s/foo/bar/’ a.pl

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing files in-place Autosplit Using modules Input record separators

Editing files in-place
Instead of just printing out the file, one thing you might want to do with the -p flag is make the changes directly to the file. Perl’s -i flag does exactly that: Print to stdout perl -pe ’s/foo/bar/’ a.pl Edit a.pl in-place perl -pi -e ’s/foo/bar/’ a.pl

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing files in-place Autosplit Using modules Input record separators

Editing files in-place
Instead of just printing out the file, one thing you might want to do with the -p flag is make the changes directly to the file. Perl’s -i flag does exactly that: Print to stdout perl -pe ’s/foo/bar/’ a.pl Edit a.pl in-place perl -pi -e ’s/foo/bar/’ a.pl Editing multiple files perl -pi -e ’s/foo/bar/’ *.pl

Walt Mankowski

Essential One-Liners

Walt Mankowski Essential One-Liners . So Perl lets you specify a backup file when using -i.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Editing files in-place Obviously the -i flag is dangerous since it clobbers whatever was originally in the file.

pl in-place perl -pi -e ’s/foo/bar/’ a.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Editing files in-place Obviously the -i flag is dangerous since it clobbers whatever was originally in the file.pl Walt Mankowski Essential One-Liners . So Perl lets you specify a backup file when using -i. Edit a.

Edit a.bak -e ’s/foo/bar/’ a.pl Walt Mankowski Essential One-Liners .pl in-place perl -pi -e ’s/foo/bar/’ a.bak perl -p -i.pl Original file saved in a. So Perl lets you specify a backup file when using -i.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Editing files in-place Obviously the -i flag is dangerous since it clobbers whatever was originally in the file.pl.

use the -F flag to split on something else. Default is to split on ’ ’.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Automatically splitting files Use the -a flag to automatically split each line (like AWK). Walt Mankowski Essential One-Liners .

use the -F flag to split on something else. Default is to split on ’ ’.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Automatically splitting files Use the -a flag to automatically split each line (like AWK). Print processes whose parents are init ps axl | perl -ane ’print if $F[3] == 1’ Walt Mankowski Essential One-Liners .

Default is to split on ’ ’. use the -F flag to split on something else. Print processes whose parents are init ps axl | perl -ane ’print if $F[3] == 1’ Print all userids and user names perl -aln -F: -e ’print "$F[2]\t$F[0]"’ /etc/passwd Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Automatically splitting files Use the -a flag to automatically split each line (like AWK).

getprint "http://pghpw.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Using modules Instead of explicitly use’ing a module.org"’ Walt Mankowski Essential One-Liners . The following programs both do the same thing: Use module perl -e ’use LWP::Simple. you can load a module from the command line with the -M flag.

One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Using modules Instead of explicitly use’ing a module. you can load a module from the command line with the -M flag. The following programs both do the same thing: Use module perl -e ’use LWP::Simple.org"’ Walt Mankowski Essential One-Liners .org"’ -M flag perl -MLWP::Simple -e ’getprint "http://pghpw. getprint "http://pghpw.

which defaults to \n and can be changed by setting $/.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Input record separators Perl normally reads input until it hits the “input record separator”. But in addition to setting $/ in a BEGIN block. It sets $/ to an octal or hex number: Walt Mankowski Essential One-Liners . you can also change it on the command line with the -0 flag.

But in addition to setting $/ in a BEGIN block. which defaults to \n and can be changed by setting $/. It sets $/ to an octal or hex number: -0x0d carriage returns Walt Mankowski Essential One-Liners . you can also change it on the command line with the -0 flag.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Input record separators Perl normally reads input until it hits the “input record separator”.

One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Input record separators Perl normally reads input until it hits the “input record separator”. It sets $/ to an octal or hex number: -0x0d -0 carriage returns null character (find -print0) Walt Mankowski Essential One-Liners . But in addition to setting $/ in a BEGIN block. which defaults to \n and can be changed by setting $/. you can also change it on the command line with the -0 flag.

which defaults to \n and can be changed by setting $/. It sets $/ to an octal or hex number: -0x0d -0 -00 carriage returns null character (find -print0) paragraph mode (useful for Postfix logs) Walt Mankowski Essential One-Liners . you can also change it on the command line with the -0 flag. But in addition to setting $/ in a BEGIN block.One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Input record separators Perl normally reads input until it hits the “input record separator”.

One-Liners 101 Advanced Perl One-Liners Summary Examples Editing files in-place Autosplit Using modules Input record separators Input record separators Perl normally reads input until it hits the “input record separator”. But in addition to setting $/ in a BEGIN block. you can also change it on the command line with the -0 flag. which defaults to \n and can be changed by setting $/. It sets $/ to an octal or hex number: -0x0d -0 -00 -0777 carriage returns null character (find -print0) paragraph mode (useful for Postfix logs) slurp in entire file Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Summary of flags Flag -e -l -n -p -i -a -M -0 Result Execute program on command line Automatically add newlines Automatically loop Automatically loop and print each line Edit files in-place Automatically split input Use module Change input record separator Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples More Information perldoc perlrun many more features than I covered Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples More Information perldoc perlrun many more features than I covered Google for “perl one liners” Tom Christiansen one-liners article by Jeff Bay in The Perl Review “One-liners 101” on IBM developerWorks many others Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Java doesn’t have one-liners Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Examples! Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Palindromes Find palindromes perl -lne ’print if $_ eq reverse’ \ /usr/share/dict/words Walt Mankowski Essential One-Liners .

$_"’ $. Walt Mankowski Essential One-Liners . is a special Perl variable that contains the input line number.One-Liners 101 Advanced Perl One-Liners Summary Examples Line Numbers Print lines preceded by line number perl -ne ’print "$.

}’ nums 2. then divide by total number of lines % perl -lne ’$s += $_. END{print $s/$.One-Liners 101 Advanced Perl One-Liners Summary Examples Computing Averages Sum lines.5 % Walt Mankowski Essential One-Liners .

operator is magic when used in scalar context.. Walt Mankowski Essential One-Liners ..20’ The .One-Liners 101 Advanced Perl One-Liners Summary Examples Printing selected lines Print lines 10–20 perl -ne ’print if 10. Read the “Range Operators” section in perlop.

One-Liners 101 Advanced Perl One-Liners Summary Examples Grep with Perl regular expressions Poor man’s grep perl -ne ’print if /^foobar/’ Walt Mankowski Essential One-Liners .

. . but with the full power of Perl’s regular expressions! Lines with foo not followed by bar perl -ne ’print if /foo(?!bar)/’ Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Grep with Perl regular expressions Poor man’s grep perl -ne ’print if /^foobar/’ .

One-Liners 101 Advanced Perl One-Liners Summary Examples Grep with Perl regular expressions Poor man’s grep perl -ne ’print if /^foobar/’ . . but with the full power of Perl’s regular expressions! Lines with foo not followed by bar perl -ne ’print if /foo(?!bar)/’ Lines with bar not preceded by foo perl -ne ’print if /(?<!foo)bar/’ Walt Mankowski Essential One-Liners . .

One-Liners 101 Advanced Perl One-Liners Summary Examples Grep with Perl regular expressions Poor man’s grep perl -ne ’print if /^foobar/’ . . . but with the full power of Perl’s regular expressions! Lines with foo not followed by bar perl -ne ’print if /foo(?!bar)/’ Lines with bar not preceded by foo perl -ne ’print if /(?<!foo)bar/’ Grep on paragraphs perl -00 -ne ’print if /foo(?!bar)/’ Walt Mankowski Essential One-Liners .

One-Liners 101 Advanced Perl One-Liners Summary Examples Random Numbers Does rand() return the same sequence with the same seed on different platforms? Walt Mankowski Essential One-Liners .

print rand for 1.744525000061007 0.0811111711783106 Walt Mankowski Essential One-Liners .111085282444161 0..5’ 0.342701478718908 0.422338957988309 0.One-Liners 101 Advanced Perl One-Liners Summary Examples Random Numbers Does rand() return the same sequence with the same seed on different platforms? Look at the first 5 % perl -le ’srand(42).

5’ 0.744525000061007 0. print rand for 1.111085282444161 0. print rand for 1.One-Liners 101 Advanced Perl One-Liners Summary Examples Random Numbers Does rand() return the same sequence with the same seed on different platforms? Look at the first 5 % perl -le ’srand(42).422338957988309 0.100_000’ \ | md5sum ac18d07f40c858bf4b23090177f6a685 Walt Mankowski Essential One-Liners ...0811111711783106 Try a whole bunch % perl -le ’srand(42).342701478718908 0.

n += 10)) do cmd="./wn_path_seq $n $SEED" echo $cmd $cmd done Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Stacking the deck One-liners can also be used in shell scripts #!/bin/bash SEED=‘perl -le ’print int rand 0xffffffff’‘ for ((n = 10. n <= 400.

txt Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Add lines to file Add line to beginning of file perl -0777 -i -ne ’print "first\n$_"’ test.

txt Same thing perl -0777 -i -pe ’$_= "first\n$_"’ test.txt Walt Mankowski Essential One-Liners .One-Liners 101 Advanced Perl One-Liners Summary Examples Add lines to file Add line to beginning of file perl -0777 -i -ne ’print "first\n$_"’ test.

txt Walt Mankowski Essential One-Liners . more obfuscated perl -0777 -i -pe ’s//first\n/’ test.txt Same thing perl -0777 -i -pe ’$_= "first\n$_"’ test.txt Same thing.One-Liners 101 Advanced Perl One-Liners Summary Examples Add lines to file Add line to beginning of file perl -0777 -i -ne ’print "first\n$_"’ test.

One-Liners 101 Advanced Perl One-Liners Summary Examples Thank you! Walt Mankowski Essential One-Liners .

Sign up to vote on this title
UsefulNot useful