perl 提取文件的指定列的代码

发布时间:2019-07-27编辑:脚本学堂
perl 提取文件的指定列的代码,问答方式给出解决方法,供大家学习参考。

perl 提取文件的指定列的代码,问答方式给出解决方法,供大家学习参考。
文本:
pool name:    None
pool name:    NetBackup
pool name:    DataStore
pool name:    CatalogBackup
pool name:    DCCBJ_PTL_EBK_COR

要求:windows环境,用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 (<INFILE>) {
      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(<DATA>){
    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(<DATA>){
    print STDOUT (split)[2],"n";
}
__DATA__
pool name:    None
pool name:    NetBackup
pool name:    DataStore
pool name:    CatalogBackup
 

也没有出现错误,等高手来解释这个问题吧

仙子回答:
像这种格式比较固定的,用regex或split都可以。命令行
 

复制代码 代码如下:
# perl -e '
my $str=<<EOF;
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