• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

perl命令行小记

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

整洁性

-w 打开警告
-Mstrict 打开严格编译指示(pragma)

数据

-0 (这是个零)指定输入记录分隔符
-a 将数据分割成名为 @F 的数组
-F 指定分割时 -a 使用的模式(请参阅 perldoc -f split)
-i 在适当的位置编辑文件(请参阅 perldoc perlrun 以获取大量详细信息)
-n 使用 <> 将所有 @ARGV 参数当作文件来逐个运行
-p 和 -n 一样,但是还会打印 $_ 的内容

执行控制

-e 指定字符串以作为脚本(多个字符串迭加)执行
-M 导入模块
-I 指定目录以搜索标准位置前的模块
 
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 的文章:



perl -lane 'print if $F[8]=~/\d+/'  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Perl入门(一)Perl的基本类型及运算符发布时间:2022-07-22
下一篇:
perl的sort函数发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap