-
-
Save noodles-v6/2233461 to your computer and use it in GitHub Desktop.
Perl oneliners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encoding file | |
perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' < file | |
# Decoding file | |
perl -MMIME::Base64 -ne 'print decode_base64($_)' < file.b64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 在1.txt中找到包含par的行,并打印 | |
# The -n switch makes perl go over the input file line by one. The code then makes it print the line and stop once the given pattern is found. | |
perl -ne 'if (/par/) { print; last; }' 1.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Perl 2 JSON | |
perl -MJSON -ple '$_=encode_json eval $_' | |
# JSON 2 Perl | |
perl -MJSON::PP -MJSON::Syck=Load -MData::Dumper -pe '$_=Dumper(Load(encode_json(JSON::PP->new->relaxed->allow_barekey->decode($_))));' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MD5 checksum (whole file) | |
perl -MDigest::Perl::MD5=md5_hex -le 'undef $/; $_=<STDIN>; print md5_hex($_)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simplest webserver | |
plackup -MYAML::Syck --port 8080 -e 'sub { [ 200, [], [Dump \%ENV] ] }' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assemble regexp from list of strings | |
perl -MRegexp::Assemble -pe 'BEGIN { $r=Regexp::Assemble->new }; $r->add($_); undef $_; END { print $r->re, "\n" }' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encode UTF-8 string as GSM 03.38 7-bit string | |
perl -MEncode -ple '$_=encode "GSM0338", decode "UTF-8", $_; $l=length($_)*2; $l -= $l>6 ? int($l/8) : 0; $b=unpack "b*", $_; $b=~s/(.{7})./$1/g; $_=sprintf "%02X%s", $l, uc unpack "H*", pack "b*", $b' | |
# Decode GSM 03.38 7-bit string to UTF-8 | |
perl -MEncode -ple 's/^(..)//; $l = (hex($1)+2)*4; $b=unpack "b*", pack "H*", $_; $b=~s/(.{7})/$1./g; $b=substr $b, 0, $l; $b=substr $b, 0, int(length($b)/8)*8; $_=decode "GSM0338", pack "b*", $b;' | |
# Encode UTF-8 as ESTI GSM 03.38 IRA string | |
perl -MEncode -ple '$_=uc unpack "H*", encode "GSM0338", decode "UTF-8", $_' | |
# Decode ESTI GSM 03.38 to UTF-8 IRA string | |
perl -MEncode -ple '$_=encode "UTF-8", decode "GSM0338", pack "H*", $_' | |
# Encode UTF-8 as UCS2-LE IRA string | |
perl -MEncode -ple '$_=uc unpack "H*", pack "n*", unpack "v*", encode "UCS-2LE", decode "UTF-8", $_' | |
# Decode UCS2-LE IRA string to UTF-8 | |
perl -MEncode -ple '$_=encode "UTF-8", decode "UCS-2LE", pack "v*", unpack "n*", pack "H*", $_' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# URL encoding by RFC 3986 (by line) | |
perl -MURI::Escape -pe '$_=uri_escape($_)' | |
perl -MURI::Escape -pe '$_=uri_unescape($_)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pretty printing in compact form | |
perl -MXML::Twig -MText::Wrap -e 'package Text::Wrap; $columns=78; $huge="overflow"; XML::Twig->new(pretty_print=>"indented_c")->parse(\*STDIN)->print;' | |
# Pretty printing with attributes in separate lines (VCS-friendly) | |
perl -MXML::Twig -MText::Wrap -e 'package Text::Wrap; $columns=78; $huge="overflow"; XML::Twig->new(pretty_print=>"indented_a")->parse(\*STDIN)->print;' | |
# XSD 2 example XML | |
perl -MXML::Compile::Schema -le '$r=shift @ARGV; $s=XML::Compile::Schema->new; $s->importDefinitions($_) for @ARGV; print $s->template("XML", $r, show=>"ALL")' '{http://some.name.space/}rootTag' *.xsd | |
# XSD 2 example YAML | |
perl -MYAML -MXML::Compile::Schema -le '$r=shift @ARGV; $s=XML::Compile::Schema->new; $s->importDefinitions($_) for @ARGV; print Dump eval $s->template("PERL", $r, show=>"ALL")' | |
# XML to YAML | |
perl -MYAML -MXML::Compile::Schema -MXML::Compile::Util=type_of_node -le '$x=XML::LibXML->new->parse_fh(\*STDIN); $r=$x->documentElement; print Dump (XML::Compile::Schema->new(\@ARGV)->compile(READER=>type_of_node($r), sloppy_integers=>1, sloppy_floats=>1)->($r));' *.xsd < file.xml | |
# YAML to XML | |
perl -MYAML=LoadFile,Dump -MXML::Compile::Schema -le | |
'$d=LoadFile(shift @ARGV); $r=shift @ARGV; $x=XML::LibXML::Document->new("1.0", "UTF-8"); $x=XML::Compile::Schema->new(\@ARGV)->compile(WRITER => $r)->($x, $d); print $x->toString(1)' file.yml '{http://some.name.space/}rootTag' *.xsd | |
# YAML to XML where data are inside hash key | |
perl -MYAML=LoadFile,Dump -MXML::Compile::Schema -le | |
'$d=LoadFile(shift @ARGV); $r=shift @ARGV; ($a=$r)=~s/{.*}//; $x=XML::LibXML::Document->new("1.0", "UTF-8"); $x=XML::Compile::Schema->new(\@ARGV)->compile(WRITER => $r)->($x, $d->{$a}); print $x->toString(1)' file.yml '{http://some.name.space/}action' *.xsd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment