perl¶
Practical Extraction and Reporting Language
Special Variables¶
- "That thing":
$_ - Record Separator:
$/
Techniques¶
Assign an array to some matches¶
Assign several variables to some matches¶
Iterate a hash¶
Print out a file with line numbers¶
This should probably be done with nl -ba .bash_history instead.
Edit a file in-place¶
To change all instances of "foo" to "bar":
Remove blank lines from a file¶
Remove lines from a file that match a certain regex¶
Sort a line by spaces¶
See bash for a bash-only way
echo -n "whiskey tango foxtrot " \
| perl -e '
$/=" " ;
@foo = <STDIN> ;
print (sort(@foo)) ;
print "\n" ;
'
Sort records in a file that are separated by a blank line¶
##!/usr/bin/perl
$/ = "\n\n" ;
my @input = (<STDIN>) ;
my @sorted = sort { lc($a) cmp lc($b) } @input ;
foreach (@sorted) {
if (length($_) > 10) { print "$_"; }
}
Subtract two from the last octet of a MAC address¶
for X in 24:b6:fd:ff:b7:f{{a..f},{0..9}} ; do
echo -n "${X} - 2 = " ;
echo ${X} \
| perl -ne '
@foo = split(":",$_) ;
$foo[5] = sprintf("%02x", (hex($foo[5]) - 2)) ;
$new = join(":",@foo) ;
print "$new\n" ;
' ;
done ;