See also https://github.com/jreisinger/blog/blob/master/posts/shell-one-liners.md

Perl one-liners are small Perl programs that are run directly from a command line (ex. on Unix/Linux, Cygwin). For a deeper dive see Famous Perl One-Liners Explained. If you want a book, have a look at Perl One-Liners or Minimal Perl for UNIX and Linux People.

Perl command line switches

See perlrun for more.

Unix tools replacements

grep replacement

sed replacement

awk replacement

dos2unix replacement

Various

Autorun script when I save it in the editor (from PM)

perl -E 'while(-f $ARGV[0]){ $now=(stat(_))[9]; system($^X,@ARGV) if($now-$prev); $prev=$now; sleep 1}' /home/user/test.pl foo bar

Encode (OSSEC keys) in base64

cat /var/ossec/etc/client.keys | perl -MMIME::Base64 -lane 'print join " ", @F[0..2], encode_base64($_,"")'

Generate a random password that is 20 characters long

perl -le 'print map { ( a..z, A..Z, 0..9, qw/! @ # $ % ^ & */ )[rand 70] } 1..20'

Total size of found files (using the Eskimo Greeting Operator as suggested by PerlMonks)

find /opt/splunk/syslog/ -iname "*log*" -type f -mtime +30 | perl -lne '$sum += (stat)[7] }{ print $sum'

Find big palindromes

perl -lne 'print if $_ eq reverse and length >= 5' /usr/share/dict/words

Print a file system tree on UNIX like systems (source)

ls -R | perl -ne'if(s/:$//){s{[^/]*/}{--}g;s/^-/\t|/;print}'

Remove comments and compress all consecutive blank lines into one (more)

cat /etc/ssh/sshd_config | perl -nle '!/^#/ and print' | perl -00 -pe ''

Create HTML anchor element

perl -le 'print "<a href=\"$ARGV[1]\">$ARGV[0]</a>"' 'Perl programming documentation' http://perldoc.perl.org/

Greet user (stolen from Utilitarian :-) )

perl -E 'say "Good " . qw(night morning afternoon evening)[(localtime)[2]/6] . ", $ENV{USER}!"'

More/Sources


CategoryPerl

PerlOneLiners (last edited 2017-07-28 08:34:32 by JozefReisinger)