perl 使用

简介:
对应脚本运用:
1.  shell  统筹管理 脚本的运行。合理结合 crontab ,  ps -ef ,kill 等命令。
2.  perl  处理短小快 。
3.  python 有比较复杂结构和逻辑的。
本文主要介绍 perl 的行级命令使用 ,力求 短 小 快 
# 最简单的
$ perl  - ' print "Hello World\n" '
# 处理文件 行
$ perl  - - ' 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  - - ' next LINE unless /pattern/; print $_ '
# 去除换行  chomp
perl  - ' 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  - - 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 "       "    - 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=9676
#一些实际 使用 :)
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  - '
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

本文转自博客园刘凯毅的博客,原文链接:perl 使用,如需转载请自行联系原博主。

目录
相关文章
|
2月前
|
Unix 程序员 Linux
Perl
Perl
13 0
|
数据库 索引 Perl
|
Perl 自然语言处理
|
Perl 数据格式 XML