CU perl精华区一些常用技巧总结
发布时间:2020-12-16 00:34:44 所属栏目:大数据 来源:网络整理
导读:转载http://www.linuxtone.org/thread-1600-1-1.html 1.查询系统已经安装的perl模块 find `perl -e 'print "@INC"'` -name '*.pm' -print 2.提取文件中连续的一段区域,且末行不显示 ??方法一:使用shell ??sed -n '/start/,/end/p' file | sed -n '$!p' ??
转载http://www.linuxtone.org/thread-1600-1-1.html find `perl -e 'print "@INC"'` -name '*.pm' -print 2.提取文件中连续的一段区域,且末行不显示 ??方法一:使用shell ??sed -n '/start/,/end/p' file | sed -n '$!p' ??方法二:使用perl ??if ( (/$start/ .. /$end/) and !/$end/ ) ??{ ? ?}; 示例:文件file.txt内容如下 + CNC menu = 网通 title = 网通 ++ shanghai menu = 上海 title = shanghai host = 210.51.63.118 ++ jiangsu menu = 江苏 title = jiangsu host = 122.193.15.2 要求:提取内容如下 ++ shanghai menu = 上海 title = shanghai host = 210.51.63.118 shell做法: [root@localhost ~]# sed -n '/++ shanghai/,/++ jiangsu/p' file.txt | sed -n '$!p' ++ shanghai menu = 上海 title = shanghai host = 210.51.63.118 perl的做法: [root@localhost ~]# cat 1.pl #!/usr/bin/perl use strict; open(FH,"file.txt"); my $start= qr/++ shanghai/; my $end= qr/++ jiangsu/; while(<FH>){ if ( /$start/../$end/ and !/$end/ ) { print $_; } } close(FH); [root@localhost ~]# ./1.pl ++ shanghai menu = 上海 title = shanghai host = 210.51.63.118 3. hash参数传递 #!/usr/bin/perl -w use strict; sub dis{ ? ?my(%hash)=@_; ? ?my $c=$hash{a}; ? ?my $d=$hash{b}; ? ?return $c*$d; } print dis(a=>6,b=>5); 输出结果是30 4.perl 特殊变量翻译中文版,这个不错 http://bbs.chinaunix.net/viewthr ... p%3Bfilter%3Ddigest 5.perl中的??" -> " 符号作用 -> 有两种用法,都和解引用有关。 第一种用法,就是解引用。 根据 -> 后面跟的符号的不同,解不同类型的引用, ->[] 表示解数组引用,->{} 表示解散列引用,->() 表示解子程序引用。 例子: $arr_ref = @array; $arr_ref->[0] 访问数组 @array 的第一个元素。 $hash_ref = %hash; $hash_ref->{foo} 访问 %hash 的 foo 分量 $sub_ref = &;test; $sub_ref->(1,2,3) 使用参数列表 (1,3) 来调用 &test 这个子程序。 第二种用法,就是调用类或者对象的方法。 格式: $obj->method(); 或者 ClassName->method(); 例如: $pop3->login( $username,$password ); my $ftp = Net::FTP->new("some.host.name",Debug => 0); 这两种用法略有不同, 假设 -> 的左操作数(就是左边那个值,如 $pop3 和 Net::FTP)是 $left,右操作数(就是右边那个值,如 login 和 new)是 $right,那么 -> 的运算规则就是: if ( ref $left 有效 ){? ? # 也就是说 $left 是个引用,而不是个裸字 ? ? $ClassName = ref $left;??# 取引用的类型,当作类名称 } else{ ? ? $ClassName = $left;? ?? ? # 直接把裸字当作类名称 } 然后调用: &{$ClassName::$right}( $left,原参数列表 ) 也就是说把类名称和右操作数拼在一起,当作子程序名称(注),并把左操作数当作第一个参数。 注:Perl 解释器要做的工作其实要比这复杂,它还要考虑到继承的问题。 6.perl 夹带(小手册),很不错 http://bbs.chinaunix.net/viewthr ... p%3Bfilter%3Ddigest 7.杀进程的一些参考 (1): $username =`whoami`; chomp($username); @aa = `ps -ef|grep $username|grep -v grep |grep -v ps|cut -c8-14`; for(@aa){ ??chomp; ??next unless /^d+$/; ? ?? ?kill 9 ? ???} (2) fuser -n tcp 80 查看80端口运行的所有进程 fuser -k -n tcp 80 杀死80端口的所有进程 lsof -i :80|grep -v PID|awk '{print "kill -9",$2}'|sh 同上 lsof -i :80|grep -v PID|awk '{print $2}'|xargs kill -9 同上 ps -fe |grep httpd|grep -v grep|awk '{print $2}'|xargs kill -9 杀死httpd所有进程 ps -fe |grep httpd|grep -v grep|awk '{print "kill -9 " $2}'|sh??杀死httpd所有进程 ps aux|awk '{if($8=="Z") print $2 }'|xargs kill -9 查找Z表示僵死状态。 8.Perl 对象 中文翻译 http://bbs.chinaunix.net/viewthr ... p%3Bfilter%3Ddigest 9.扫描开放的tcp端口脚本 #!/usr/bin/perl $|=1; $tghost = shift || $ENV{"HOSTNAME"}; $maxprt = shift || 1024; $AF_INET=2; $SOCK_STREAM=1; $sockaddr='S n a4 x8'; ($name,$aliases,$proto)=getprotobyname('tcp'); foreach $port (1 .. $maxprt) { ? ?? ???($name,$type,$len,$thataddr)=gethostbyname($tghost); ? ?? ???$this=pack($sockaddr,$AF_INET,$thisaddr); ? ?? ???$that=pack($sockaddr,$port,$thataddr); ? ?? ??? ? ?? ???die "unknown host $tghostn" if($thataddr eq ""); ? ?? ???socket(S,$SOCK_STREAM,$proto) or die $!; ? ?? ???bind(S,$this) or die $!;? ?? ??? ? ?? ???if(connect(S,$that)) ? ?? ???{ ? ?? ?? ?? ?? ? ($srv_name,$srv_aliases,undef,undef)=getservbyport($port,'tcp'); ? ?? ?? ?? ?? ? printf("%5d %sn",$srv_name || "unknown serv"); ? ?? ?? ?? ?? ? close(S); ? ?? ???} } exit 0; 10. perl 脚本语法检查 perl -c script.pl 11. 查看shell脚本运行过程 sh -x??script.sh (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |