perl example!
example3: my a=OFC33L40.0 perl '/(d+)(?=D)/ and print $1' $ example4: #!/opt/exp/bin/perl use strict; while(<>){ # s/Q[0-9]E(?:[^+*/])/[0-9]+/g; s/[0-9](?:[^+*/])/[0-9]+/g; print; } #!/opt/exp/bin/perl use strict; my $line = 0; while(<>){ $line++; print "line7 line=$line n"; if(s/d01/sprintf("d%02d",$line)/ge){ print; } } example 5: remove felixzh1 #!/usr/bin/perl -w use strict; $^I = '.bak'; #read ARGV (read command line parameter file one line by one line and one file by one file) while (<ARGV>) { s/felixzh1//g; print; } it is the same with perl command line perl -i.bak -pe 's/felixzh1//g' filename example 6: 删除felixzh1所在的行 -n相当于-p只是不打印原来的文件内容。 -p会把每一行打出来 perl -n -e 'print unless(/felixzh1/);' < file perl -n -e '/felixzh1/ || print' < file perl -n -e 'print unless(/felixzh1/);' < file perl -e 'while(<>){print unless(/felixzh1/);}' < file perl -n -e 'print unless(/felixzh1/);' < 1.txt >2.txt 6.1 在diskinfo的上一行加#add by abc,在其下一行加'print (stderr,"abc line111 n")',#!/usr/bin/perl my $dist = 'diskinfo'; while (<DATA>) { print /$dist/ ? ( "#add by abcn",$_,'print (stderr,"n" ) : $_; } __DATA__ fdfa sort_diskinfo() print1 # perl -lape '$_="#add by abcn$_nprint (stderr,"abc line111n")" if /diskinfo/' 1.txt (la即lane可以实验去掉看看) example 7: class 文本: $ cat 9.txt 例子分析 #!/opt/exp/bin/perl -w use strict; my $file=$ARGV[0]; my $line; my @tmp; my $fh; my %uniq; my @list; open($fh,'<',$file) or die $!; print "line6:$file n"; while ($line=<$fh>) { print "line8:$line "; #don;t need add n,because perl read each line include "n" print "line9:$_ n"; #string or list change to array # error1 @tmp=$line,数组将仅有一个元素. @tmp = split(/-/,$line);数组将存line分割出来的各个元素 @tmp = split(/-/,$line); #输出数组 print "line10:@tmp n"; # print 最后元素(从最后向前print 元素) print "line11:$tmp[-1];$tmp[-2];$tmp[-3] n"; #不会匹配第一个“- ”,因为有$限制 $line =~ s/-([^-]+)$//; print "line12:$line n"; #将每行的值放入数组保持 push @list,$line; } #grep 当成一个循环,uniq{$_} 为0,取反为1,为1则放入list中,为0则不放入; # uniq 是一个哈希.哈希可以将string做为下标.?? 然后$uniq{$_}++;这样去掉了重复的行 # sort 按字母排序 my @output = sort(grep {!$uniq{$_}++} @list); # 因为在$line =~ s/-([^-]+)$//;此处,string的n也被用空替换了,所以各个数组元素之间,要加上n区别开 # join 返回值是string,将数组变成了带n的一个string print join("n",@output); =================== example 8: $ cat 10.txt wang 12 qian 45 67 sun 22 31 tang 77 89 43 li nan 78 将数字乘以2 $ cat number.pl #!/opt/exp/bin/perl -w my $file = $ARGV[0]; my @tmp; my $fh; my $i; open ($fh,$file) or die $!; while (my $line=<$fh>) { @tmp = split(/s+/,$line); print "line6:@tmp"; #在正则中e表示执行函数,此句表示将数字用函数运算的结果,替换 $line =~ s/(d+)/$1*2/eg; print "line16:$line n"; } example 9: perl 批量更改文件名 [root@localhost test]# ls *.txt a.txt b.txt [root@localhost test]# perl -e 'foreach(<*.txt>) {s/.txt$//;rename(qq{$_.txt},qq{$_-2011.txt})}'-------------------此处s/.txt$// 获取文件除去后缀名的文件名a,b [root@localhost test]# ls *.txt a-2011.txt b-2011.txt 简单注释:如果文件名放到运算符(<>)中,perl会自动支持globbing。glob函数与运算符<*>点作用是完全相同的。 [root@localhost test]# perl -e 'foreach(glob("*.txt")) {s/.txt$//;rename(qq{$_.txt},qq{$_-2011.txt})}' [root@localhost test]# ls *.txt a-2011-2011.txt b-2011-2011.txt example 10: 删除某行,删除一行 方法1: perl -ne 'print if ! /^sky/' record.txt chihao 22 secret 提交 alien 25 male 提交 sky 20 secret 提交 方法2: $ perl -we ' use strict; open(FN,"test.txt"); while(<FN>){ next if /&;test/; print; } 方法3: open(FILE,"<a.txt"); @text=<FILE>; open(FILL,">b.txt") for(my $i=0; $i < @text; $i++) { if($text[$i] !~/bug/){ print FILL $text[$i]; } } close FILL; close FILE; 如果是空行,直接将$text[$i] !~/bug/ 替换成 $text[$i] eq "" example 11: <*> 应该是glob,类似shell里面ls所有的文件 print <> ; cat 的perl实现,print sort <> ; sort的perl实现 正则 ^但是使用表示行首,^.后接字符表示以该字符开头 example12: perl eval 类似java中的try{}catch{} 语句,perl中也可以捕获程序执行过程中的异常,防止因为局部异常导致程序全盘退出。 my $a=0; my $b=1; my $c=1; eval { $c = $b/$a; }; print "An error occurred:$@" if $@ $@ 存放正常,异常的info 注意与shell 中eval的区别 foo=10 x=foo eval y=’$’$x echo $y eval可读取一连串的参数,然后再依参数本身的特性来执行 参数不限数目,彼此之间用分号分开。 example14: perl在制定行前添加一行 <span style="font-family: Arial,Helvetica,sans-serif; background-color: rgb(255,255,255);"></span><pre code_snippet_id="637891" snippet_file_name="blog_20150407_6_7119606" name="code" class="html"><span style="color: rgb(51,51,51); font-family: Arial; font-size: 14px; line-height: 26px;">njcgpb:felixzh/temp/perl >> cat addline.pl</span> <p style="color: rgb(51,51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><pre name="code" class="html">#!/opt/exp/bin/perl -w foreach my $file (glob "*.tcl") { print "$filen"; &max($file); } sub max { my $dist = 'WISEOpt'; my $line = 0; my $i = 0; my $f1 = shift @_; my $f2 = $f1.".bak"; my $fd; my $fd2; open $fd,"<$f1"; open $fd2,">$f2"; while (<$fd>) { $i = $i +1; $line = $i; print $fd2 /$dist/ ? ( "exec echo "felixzh1 $line C(RCS${rcs}WISEOpt)=$C(RCS${r cs}WISEOpt)>>/bld/felixzh/1n"","n") : $_; } close $fd; close $fd2; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |