對應腳本運用:
1. shell 統籌管理 腳本的運行。合理結合 crontab , ps -ef ,kill 等命令。
2. perl 處理短小快 。
3. python 有比較復雜結構和邏輯的。
本文主要介紹 perl 的行級命令使用 ,力求 短 小 快

:
#最簡單的
$ perl -e 'print "Hello World\n"'
#處理文件 行
$ perl -n -e 'print $_' file1

#編碼轉換
#如果 有需要 在使用下 encode("UTF-8", decode("GBK",$_));在 linux 下默認 utf-8
perl -MEncode -ne 'print decode("GBK",$_);' file.txt


#正則使用
# if($_=~/.*\/(.*)$/){ print $1 ;} 這是perl 巨方便的地方 $1 xx
# next LINE 跳到下一個循環
$ perl -n -e 'next LINE unless /pattern/; print $_'
#去除換行 chomp
perl -e 'print split(/\n/,"asg\n");'
#像 awk 一樣 Begin End
$ perl -ne 'END { print $t } @w = /(\w+)/g; $t += @w' file.txt
#像 awk -F"x" 一樣 切割行
#-a 打開自動分離 (split) 模式
#@F 為 切割后的 數組
perl -F'\t' -ane '
if($F[1]=~/侃侃/ and $F[2]=~/愛情啊/){
print "$F[3]\t$F[4]\t$F[5]\n"
}
' all2_data.sort.st

實際處理:
perl -F'\|\|' -ane '
my $actor,$music ;
if ( $F[3] ){
$music=$F[2];
$actor=$F[3];
}else{
$music=$F[0];
$actor=$F[1];
}
$music =~ tr/[A-Z]/[a-z]/;
$music =~ s/\s*(.*)\s*\(.*\)/\1/g;
$actor =~ tr/[A-Z]/[a-z]/;
$actor =~ s/\s*(.*)\s*\(.*\)/\1/g;
print "$actor-$music\n";
' ring.utf8.txt |sort -u > ring.actor_music.sort.utf8.txt &
wc -l ring.actor_music.sort.utf8.txt
#像 sed 一樣替換
# -i 和 sed 一樣 perl 輸出會替換調 源 file.txt
$ perl -i -pe 's/\bPHP\b/Perl/g' file.txt
#外部 傳參
perl -ne'print "$ARGV[0]\t$ARGV[1]\n" ' file.txt 'par1' 'par2'
#結果 .. par1 par2 ..
# 查詢出 重復列 次數,并 列舉出來
cut -d" " -f 2 .collection_mobile.data |perl -ne '
END{
while (($key,$value)=each(%a)){print $key,"=",$value,"\n";};
}BEGIN{ %a =(); }
chomp;
$a{$_}+=1;
'
結果
Ring=532895
CRBT=68500
RingBoxes=880
Song=96765
#一些實際使用 :)
find . -name "*.mp3" | perl -pe 's/.\/\w+-(\w+)-.*/$1/' | sort | uniq
perl -F'\t' -ane 'if($F[1]=~/$ARGV[0]/ and $F[2]=~/$ARGV[1]/){print "$F[3]\t$F[4]\t$F[5]\n"}' all2_data.sort.st '侃侃' '愛情啊'
#與 find 合用 -e $ARGV[0] 批量 把 excel 倒成 文本 格式
find . -maxdepth 1 -name "*xls" -exec perl -e '
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl");
my $file=$ARGV[0];
sub myRead{
my $str = "";
for $tmp(@_){
$str="$str$tmp\t";
}
$str="$str\n";
system "echo \"$str\" >> $file.data ";
}
&parse_excel("$file",0,\&myRead);
print "$file\n";
' {} \;
參考:
http://www.ibm.com/developerworks/cn/linux/sdk/perl/l-p101/index.html
http://bbs.chinaunix.net/viewthread.php?tid=499434
整理 m.tkk7.com/Good-Game