问题:
各位Perl 老大
看下面的文本
pool name: None
pool name: NetBackup
pool name: DataStore
pool name: CatalogBackup
pool name: DCCBJ_PTL_EBK_COR
很弱的一个问题,(我在windows环境,所以想用 Perl)如何用Perl实现,我可以得到第二列的值 ?
None
NetBackup
DataStore
CatalogBackup
DCCBJ_PTL_EBK_COR
多谢了?
回答:
给你一个通用的命令程序,
:
[liuguiyou@localhost ~]$ cat a.txt
pool name: None
pool name: NetBackup
pool name: DataStore
pool name: CatalogBackup
pool name: DCCBJ_PTL_EBK_COR
[liuguiyou@localhost perl]$ cat tiqu_lie.pl
#!/usr/bin/perl
#提取某些列,增加表头,按照条件输出
use warnings;
use strict;
open(INFILE, "/home/liuguiyou/a.txt");
open(OUTFILE, ">/home/liuguiyou/b.txt") || die "Cannot open the newfile: $!\n";
my @a;
while () {
chomp;
@a = split;
print OUTFILE "$a[2]\n";
}
exit;
[liuguiyou@localhost ~]$ cat b.txt
None
NetBackup
DataStore
CatalogBackup
DCCBJ_PTL_EBK_COR
b.txt就是你想要的!
回答
#!/usr/bin/perl
while(
){
print for (split)[2],"\n"; #"for" can be replaeced by "foreach";
}
__DATA__
pool name: None
pool name: NetBackup
pool name: DataStore
pool name: CatalogBackup
个人感觉这样更好点,输出的结果为:
None
NetBackup
DataStore
CatalogBackup
__DATA__表示的是__DATA__下面的那些数据,另外print for (split)[2];是高手们的一种写法,意思是:输入DATA的每一行,以空格分割,得到一个数组,打印出数组的第2个元素,省略掉的都是默认变量$_
如果提取1列和2列,只需要将 (split)[2],改为 (split)[1,2],即可。
说明:有网友建议把print for (split)[2],"\n";中的for去掉,是多余的,直接改为print + (split)[2],"\n",自己把上边的改为:
#!/usr/bin/perl
use warnings;
use strict;
while(){
print STDOUT (split)[2],"\n";
}
__DATA__
pool name: None
pool name: NetBackup
pool name: DataStore
pool name: CatalogBackup
也没有出现错误,等高手来解释这个问题吧
仙子回答:
像这种格式比较固定的,用regex或split都可以。命令行
# perl -e '
my $str=<pool name: None
pool name: NetBackup
pool name: DataStore
pool name: CatalogBackup
pool name: DCCBJ_PTL_EBK_COR
EOF
open my $fd,"<",\$str;
while(<$fd>) {
print $1,"\n" if /^pool name:\s+(.+)$/
}
'
None
NetBackup
DataStore
CatalogBackup
DCCBJ_PTL_EBK_COR
阅读(2560) | 评论(0) | 转发(0) |