perl 命令行小记
整潔性
| -w | 打開警告 |
| -Mstrict | 打開嚴格編譯指示(pragma) |
數據
| -0 | (這是個零)指定輸入記錄分隔符 |
| -a | 將數據分割成名為 @F 的數組 |
| -F | 指定分割時 -a 使用的模式(請參閱 perldoc -f split) |
| -i | 在適當的位置編輯文件(請參閱 perldoc perlrun 以獲取大量詳細信息) |
| -n | 使用 <> 將所有 @ARGV 參數當作文件來逐個運行 |
| -p | 和 -n 一樣,但是還會打印 $_ 的內容 |
執(zhí)行控制
| -e | 指定字符串以作為腳本(多個字符串迭加)執(zhí)行 |
| -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 系統(tǒng)管理(?developerWorks,2001 年 7 月)
- JAPH 的精致(?developerWorks,2001 年 7 月)
- Perl 用于實現遺傳算法(?developerWorks,2001 年 8 月)
- 用 Perl 讀寫 Excel 文件(?developerWorks,2001 年 9 月)
- 介紹用于系統(tǒng)管理的 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 命令行小记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vim as Python IDE on
- 下一篇: JSP利用servlet上传文件时的中文