Useful Perl one liners

Search and replace

  • Search in file and replace a with b

perl -p -e 's/a/b/g' datei
  • Replace and make backup before

perl -p -i.old -e 's/a/b/g' datei
  • Recursivly replace files

find . -type f | xargs perl -p -e 's/a/b/g'

ASCII, Hex and Binary conversion

  • ASCII2Hex (urlencoded)

perl -e 'map { print "%"; printf("%x",ord($_)); } split(//,$ARGV[0]); print"\n";'
  • Hex2ASCII

perl -e 'map { print chr(hex($_)); } split(/%/,$ARGV[0]); print "\n";'
  • ASCII2Binary

perl -e 'map { print "$_ "; printf("%08b",ord($_)); print "\n"; } split(//,$ARGV[0]);'
  • Binary2ASCII

perl -e 'print chr(ord(pack("B*",$ARGV[0])))."\n";'

Base64

  • Encoding

perl -MMIME::Base64 -e 'print MIME::Base64::encode_base64($ARGV[0]) . "\n"' "BLA BLA BLA"
  • Decoding

perl -MMIME::Base64 -e 'print MIME::Base64::decode_base64($ARGV[0]) . "\n"' "QkxBIEJMQSBCTEE="

Make /etc/hosts to domain names

perl -n -e 'my @a=split(/\s*\s/,$_); print "$a[1]\tIN\tA\t$a[0]\n";' /etc/hosts >> domain.name.fwd