1、查找 Artist-Album-Track#-Song.mp3 的专辑名
> find . -name "*.mp3" | perl -pe 's/.\/\w+-(\w+)-.*/$1/' | sort | uniq
2、在文件中插入行号
perl -pi -e'$_ = sprintf "%04d %s", $., $_' test
3、代替AWK
perl -lane 'print $F[0] + $F[-2]'
4、打印一系列行
# 1. just lines 15 to 17
perl -ne 'print if 15 .. 17
# 2. just lines NOT between line 10 and 20
perl -ne 'print unless 10 .. 20'
# 3. lines between START and END
perl -ne 'print if /^START$/ .. /^END$/'
# 4. lines NOT between START and END
perl -ne 'print unless /^START$/ .. /^END$/'
5、更有效地打印数字范围中的行
# just lines 15 to 17, efficiently
perl -ne 'print if $. >= 15; exit if $. >= 17;'
6、进行适当的编辑
# 1. in-place edit of *.c files changing all foo to bar
perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c
# 2. delete first 10 lines
perl -i.old -ne 'print unless 1 .. 10' foo.txt
# 3. change all the isolated oldvar occurrences to newvar
perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy]
# 4. increment all numbers found in these files
perl -i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 ....
# 5. delete all but lines between START and END
perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt
# 6. binary edit (careful!)
perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape
7、文件颠倒排列的变化情况
# 1. command-line that reverses the whole input by lines
# (printing each line in reverse order)
perl -e 'print reverse <>' file1 file2 file3 ....
# 2. command-line that shows each line with its characters backwards
perl -nle 'print scalar reverse $_' file1 file2 file3 ....
# 3. find palindromes in the /usr/dict/words dictionary file
perl -lne '$_ = lc $_; print if $_ eq reverse' /usr/dict/words
# 4. command-line that reverses all the bytes in a file
perl -0777e 'print scalar reverse <>' f1 f2 f3 ...
# 5. command-line that reverses each paragraph in the file but prints
# them in order
perl -00 -e 'print reverse <>' file1 file2 file3 ....
8、用随机数重写
# replace string XYZ with a random number less than 611 in these files
perl -i.bak -pe "s/XYZ/int rand(611)/e" f1 f2 f3
9、揭示几个文件的基本性质
# 1. Run basename on contents of file
perl -pe "s@.*/@@gio" INDEX
# 2. Run dirname on contents of file
perl -pe 's@^(.*/)[^/]+@$1\n@' INDEX
# 3. Run basename on contents of file
perl -MFile::Basename -ne 'print basename $_' INDEX
# 4. Run dirname on contents of file
perl -MFile::Basename -ne 'print dirname $_' INDEX
10、移动或重命名,它们在 UNIX 中是完全相同的操作
# 1. write command to mv dirs XYZ_asd to Asd
# (you may have to preface each '!' with a '\' depending on your shell)
ls | perl -pe 's!([^_]+)_(.)(.*)!mv $1_$2$3 \u$2\E$3!gio'
# 2. Write a shell script to move input from xyz to Xyz
ls | perl -ne 'chop; printf "mv $_ %s\n", ucfirst $_;'
11、替换ip地址
#!/usr/bin/perl -w
use Regexp::Common qw/net/; # provides the regular expressions for IP matching
my $replacement = shift @ARGV; # get the new IP address
die "You must provide $0 with a replacement string for the IP 111.111.111.111"
unless $replacement;
# we require that $replacement be JUST a valid IP address
die "Invalid IP address provided: [$replacement]"
unless $replacement =~ m/^$RE{net}{IPv4}$/;
# replace the string in each file
foreach my $file ($0, qw[/etc/hosts /etc/defaultrouter /etc/ethers], @ARGV)
{
# note that we know $replacement is a valid IP address, so this is
# not a dangerous invocation
my $command = "perl -p -i.bak -e 's/111.111.111.111/$replacement/g' $file";
print "Executing [$command]\n";
system($command);
} 请阅读 Ted 编写的“功能丰富的 Perl”系列的其它有关 Perl 的文章: