日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

perl 命令行小记

發(fā)布時間:2025/6/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 perl 命令行小记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

整潔性

-w打開警告
-Mstrict打開嚴格編譯指示(pragma)

數(shù)據(jù)

-0(這是個零)指定輸入記錄分隔符
-a將數(shù)據(jù)分割成名為 @F 的數(shù)組
-F指定分割時 -a 使用的模式(請參閱 perldoc -f split)
-i在適當(dāng)?shù)奈恢镁庉嬑募?#xff08;請參閱 perldoc perlrun 以獲取大量詳細信息)
-n使用 <> 將所有 @ARGV 參數(shù)當(dāng)作文件來逐個運行
-p和 -n 一樣,但是還會打印 $_ 的內(nèi)容

執(zhí)行控制

-e指定字符串以作為腳本(多個字符串迭加)執(zhí)行
-M導(dǎo)入模塊
-I指定目錄以搜索標(biāo)準(zhǔn)位置前的模塊
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、更有效地打印數(shù)字范圍中的行 # just lines 15 to 17, efficiently perl -ne 'print if $. >= 15; exit if $. >= 17;' 6、進行適當(dāng)?shù)木庉?/strong> # 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、用隨機數(shù)重寫

# 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、揭示幾個文件的基本性質(zhì)


# 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”系列的其它有關(guān) Perl 的文章:

  • 用 Perl 模塊進行解析(?developerWorks,2000 年 4 月)
  • Perl:化繁為簡?(?developerWorks,2000 年 6 月)
  • 用 Perl 保存(?developerWorks,2000 年 7 月)
  • 編寫說英語的 Perl 程序(?developerWorks,2000 年 8 月)
  • 《Programming Perl》第三版簡介(?developerWorks,2000 年 9 月)
  • 輕松調(diào)試 Perl?(?developerWorks,2000 年 11 月)
  • 用 Perl 進行應(yīng)用程序配置(?developerWorks,2000 年 10 月)
  • 吸引 C 和 Java 程序員目光的 Perl 5.6?(?developerWorks,2001 年 1 月)
  • 程序員面向 Linux 的設(shè)置?(?developerWorks,2001 年 3 月)
  • 一行程序 101(?developerWorks,2001 年 4 月)
  • 使用 Perl 自動化 UNIX 系統(tǒng)管理(?developerWorks,2001 年 7 月)
  • JAPH 的精致(?developerWorks,2001 年 7 月)
  • Perl 用于實現(xiàn)遺傳算法(?developerWorks,2001 年 8 月)
  • 用 Perl 讀寫 Excel 文件(?developerWorks,2001 年 9 月)
  • 介紹用于系統(tǒng)管理的 cfengine(?developerWorks,2002 年 2 月)
  • 用 Perl 進行應(yīng)用程序配置,第 2 部分(?developerWorks,2002 年 7 月)?



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

轉(zhuǎn)載于:https://www.cnblogs.com/agostop/archive/2012/03/09/2387775.html

總結(jié)

以上是生活随笔為你收集整理的perl 命令行小记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。