perl 命令行小记
整洁性
-w | 打开警告 |
-Mstrict | 打开严格编译指示(pragma) |
数据
-0 | (这是个零)指定输入记录分隔符 |
-a | 将数据分割成名为 @F 的数组 |
-F | 指定分割时 -a 使用的模式(请参阅 perldoc -f split) |
-i | 在适当的位置编辑文件(请参阅 perldoc perlrun 以获取大量详细信息) |
-n | 使用 <> 将所有 @ARGV 参数当作文件来逐个运行 |
-p | 和 -n 一样,但是还会打印 $_ 的内容 |
执行控制
-e | 指定字符串以作为脚本(多个字符串迭加)执行 |
-M | 导入模块 |
-I | 指定目录以搜索标准位置前的模块 |
# 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+/'
转载于:https://www.cnblogs.com/agostop/archive/2012/03/09/2387775.html
perl 命令行小记相关推荐
- perl 命令行备注
参考链接 Perl 常用命令行选项 unix 常用命令 perl 实现 sed awk tr nl perl 和sed,awk,tr,grep,nl等常用命令替换 sed task sed perl ...
- linux下perl命令行参数,Perl One-Liners | Perl命令行学习1 -e参数
注:本内容需要点的perl编程基础,最好是读过<perl语言入门>. 本系列是自己平常学习工作中的总结,每一个实例均为我为了讲解而设置的,自己试过的,如有错误,望能见谅 Perl 命令行参 ...
- Perl命令行常见用法及技巧
Perl命令行常见用法及技巧 作者:懒人运维 来源: 懒人运维 替换 将所有C程序中的foo替换成bar,旧文件备份成.bak perl -p -i.bak -e 's/\bfoo\b/bar/g ...
- @ARGV:perl命令行参数
当perl脚本运行时,从命令行上传递给它的参数存储在内建数组@ARGV中,@ARGV是PERL默认用来接收参数的数组,可以有多个参数,$ARGV[0]是表示接收到的第一个参数,$ARGV[1]表示第二 ...
- Perl命令行应用介绍
作 者: Dave Cross 发 表:August 10, 2004 原 名: Perl Command-Line Options 原 文:http://www.perl.com/pub/a/200 ...
- 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数
我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参数 ...
- Perl 交互命令行参数
Perl 命令行读入多个文件 代码 #! /usr/bin/perl -w use FindBin qw($Bin $Script); use Getopt::Std; use File::Path; ...
- perl模块Getopt::Std用法及实例-从命令行读取参数模块
Getopt::Std模块的使用: 初始设置: 在程序中加入如下代码: use Getopt::Std; use vars qw($opt_d $opt_f $opt_p); getopts('d:f ...
- Vivado Tcl命令行模式小记
Vivado Tcl命令行模式小记 在Tcl Console下运行 作为参数运行 实用tcl脚本 在Tcl Console下运行 使用vivado自带的tcl console运行Tcl脚本.在终端窗口 ...
最新文章
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
- 另辟蹊径,中科院自动化所等首次用图卷积网络解决语义分割难题
- 一对多和多对一的关系,用mybatis写
- RocketMQ:消息ACK机制源码解析
- 安卓虚拟机_安卓虚拟机(*New*)v1.1.31去广告/去推荐/Mod/精简/VIP版
- C语言的main函数到底怎么写的
- cloud foundry_将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法
- 三、系统的开关机和PDC简介
- java基础英语---第二十四天
- windows环境下安装npm、cnpm、bower
- 微信也QQ服务器,妄想山海QQ区还是微信区好 平民服务器选择推荐
- 酷派大观4 8970 刷android 4.4,酷派大观4电信版如何刷机?【图文教程】
- 基于java springboot的图书管理系统设计和实现
- dell inspiron 只有一个飞行模式 没有wifi_教你电脑连不上wifi的解决方法
- 极视角与山东港口科技集团青岛有限公司共建「AI 赋能智慧港口联合实验室」
- html文档半结构化数据,什么是半结构化数据?
- 服务器中搭建OA系统,云服务器搭建oa系统
- 《百度apollo》规划一
- 相比DCMM,DMBOK为什么没有数据标准?
- hac 估计 matlab程序,CV算法:K-means 、HAC、Mean Shift Clustering