Perl,读取文本文件,保存列以使用列的元素打开其他文件
Perl, reading text files, saving columns for using elements of columns to open other files
我有一个文本文件,制表符分隔,如下:
< this is a header
col1 col2 col3
blablabla text1.txt blablabla
blablabla text2.txt blablabla
blablabla text3.txt blablabla
我希望能够提取某些元素(列),在本例中仅提取单词 text1.txt、text2.txt 和 text3.txt。我想稍后使用它们打开具有这些名称的文件。
到目前为止我有代码:
#!/usr/bin/perl
use strict;
use warnings;
my @fields;
my ($column1, $column2, $column3);
my $text = "text.txt";
open(FILE, $text) or die "Could not read from $text, program halting.";
my @files;
while(<FILE>)
{
chomp;
/^</ and next;
/^\s*$/ and next;
/line*/ and next;
($column1, $column2, $column3) = split('\s', $_);
#PRINT ONE
#print $column2, "\t";
}
#PRINT TWO
print $column2, "\t";
close FILE;
如果我按照#PRINT ONE 的注释进行打印,我只得到正确的版本作为输出,包含所有三个元素,但是当我尝试将它保存在另一个变量中或将它写入文件时,只有 "text3.txt" 仍然存在。
如果我像#PRINT TWO 那样打印,我只会得到一个元素,相同的 text3.txt。
我如何从中学习?我已经尝试了很多来自该站点的代码,但没有结果,所以 far.Thank 你。
发生这种情况是因为您在循环的每一遍都覆盖了 $column2
,然后在您离开循环后,您得到了最后的结果 (text3.txt
)。
您可以在循环内写入文件,如本例所示。它还展示了如何使用正确的 3-arg open
,以及词法文件句柄:
use warnings;
use strict;
my $input_file = 'data.txt';
my $output_file = 'out.txt';
open my $fh, '<', $input_file or die $!;
open my $wfh, '>', $output_file or die $!;
while (<$fh>){
chomp;
next if /^\</;
next if /^\s*$/;
my ($c1, $c2, $c3) = split /\s/, $_;
print $wfh "$c2\n";
}
将此作为输入文件:
< this is a header
col1 col2 col3
blablabla text1.txt blablabla
blablabla text2.txt blablabla
blablabla text3.txt blablabla
产生:
col2
text1.txt
text2.txt
text3.txt
...在输出文件中。您必须弄清楚如何过滤第一行。
您还可以在循环中将输出保存到一个数组,然后再进行处理:
use warnings;
use strict;
my $input_file = 'data.txt';
open my $fh, '<', $input_file or die $!;
my @saved_entries;
while (<$fh>){
chomp;
next if /^\</;
next if /^\s*$/;
push @saved_entries, (split /\s/, $_)[1];
}
for (@saved_entries){
print "$_\n";
}
...然后您可以将其写入文件或执行您需要的操作。
请注意,我在这里过于冗长,尽可能接近 OP 代码。
我有一个文本文件,制表符分隔,如下:
< this is a header
col1 col2 col3
blablabla text1.txt blablabla
blablabla text2.txt blablabla
blablabla text3.txt blablabla
我希望能够提取某些元素(列),在本例中仅提取单词 text1.txt、text2.txt 和 text3.txt。我想稍后使用它们打开具有这些名称的文件。 到目前为止我有代码:
#!/usr/bin/perl
use strict;
use warnings;
my @fields;
my ($column1, $column2, $column3);
my $text = "text.txt";
open(FILE, $text) or die "Could not read from $text, program halting.";
my @files;
while(<FILE>)
{
chomp;
/^</ and next;
/^\s*$/ and next;
/line*/ and next;
($column1, $column2, $column3) = split('\s', $_);
#PRINT ONE
#print $column2, "\t";
}
#PRINT TWO
print $column2, "\t";
close FILE;
如果我按照#PRINT ONE 的注释进行打印,我只得到正确的版本作为输出,包含所有三个元素,但是当我尝试将它保存在另一个变量中或将它写入文件时,只有 "text3.txt" 仍然存在。 如果我像#PRINT TWO 那样打印,我只会得到一个元素,相同的 text3.txt。 我如何从中学习?我已经尝试了很多来自该站点的代码,但没有结果,所以 far.Thank 你。
发生这种情况是因为您在循环的每一遍都覆盖了 $column2
,然后在您离开循环后,您得到了最后的结果 (text3.txt
)。
您可以在循环内写入文件,如本例所示。它还展示了如何使用正确的 3-arg open
,以及词法文件句柄:
use warnings;
use strict;
my $input_file = 'data.txt';
my $output_file = 'out.txt';
open my $fh, '<', $input_file or die $!;
open my $wfh, '>', $output_file or die $!;
while (<$fh>){
chomp;
next if /^\</;
next if /^\s*$/;
my ($c1, $c2, $c3) = split /\s/, $_;
print $wfh "$c2\n";
}
将此作为输入文件:
< this is a header
col1 col2 col3
blablabla text1.txt blablabla
blablabla text2.txt blablabla
blablabla text3.txt blablabla
产生:
col2
text1.txt
text2.txt
text3.txt
...在输出文件中。您必须弄清楚如何过滤第一行。
您还可以在循环中将输出保存到一个数组,然后再进行处理:
use warnings;
use strict;
my $input_file = 'data.txt';
open my $fh, '<', $input_file or die $!;
my @saved_entries;
while (<$fh>){
chomp;
next if /^\</;
next if /^\s*$/;
push @saved_entries, (split /\s/, $_)[1];
}
for (@saved_entries){
print "$_\n";
}
...然后您可以将其写入文件或执行您需要的操作。
请注意,我在这里过于冗长,尽可能接近 OP 代码。