设为首页 收藏本站
查看: 2283|回复: 0

[经验分享] perl 命令行小记

[复制链接]

尚未签到

发表于 2015-12-26 07:57:39 | 显示全部楼层 |阅读模式
  整洁性
-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 模块进行解析( developerWorks,2000 年 4 月)
  • Perl:化繁为简 ( developerWorks,2000 年 6 月)
  • 用 Perl 保存( developerWorks,2000 年 7 月)
  • 编写说英语的 Perl 程序( developerWorks,2000 年 8 月)
  • 《Programming Perl》第三版简介( developerWorks,2000 年 9 月)
  • 轻松调试 Perl ( developerWorks,2000 年 11 月)
  • 用 Perl 进行应用程序配置( developerWorks,2000 年 10 月)
  • 吸引 C 和 Java 程序员目光的 Perl 5.6 ( developerWorks,2001 年 1 月)
  • 程序员面向 Linux 的设置 ( developerWorks,2001 年 3 月)
  • 一行程序 101( developerWorks,2001 年 4 月)
  • 使用 Perl 自动化 UNIX 系统管理( developerWorks,2001 年 7 月)
  • JAPH 的精致( developerWorks,2001 年 7 月)
  • Perl 用于实现遗传算法( developerWorks,2001 年 8 月)
  • 用 Perl 读写 Excel 文件( developerWorks,2001 年 9 月)
  • 介绍用于系统管理的 cfengine( developerWorks,2002 年 2 月)
  • 用 Perl 进行应用程序配置,第 2 部分( developerWorks,2002 年 7 月)
  
  
  perl -lane 'print if $F[8]=~/\d+/'  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156379-1-1.html 上篇帖子: [转] 一篇不错的Perl-LWP文档 下篇帖子: [译]Perl中的数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表