[英]How do I extract specific data from a .txt file in perl
I have some text file which have data in the following format: 我有一些文本文件,其数据格式如下:
Summary:
xyz
Configuration:
abc
123
Tools:
pqr
456
The tags 'Summary:', 'Configuration:' and 'Tools:' remain the same for all the files and just the content below that changes. 所有文件的标签“摘要:”,“配置:”和“工具:”保持不变,只是下面的内容发生了变化。 I need to extract just the content from the text file and print it out. 我只需要从文本文件中提取内容并打印出来。
Thanks. 谢谢。
How about something like: 怎么样:
open(FH, '<myfile.txt');
while(<FH>)
{
print $_ unless /Summary:|Configuration:|Tools:/;
}
You'll have to cleanup the regex a bit, but that should do it. 您必须稍微清理一下正则表达式,但这应该可以做到。
use strict;
use warnings;
my (%h, $header);
while (<>) {
if (/^(\w+):) {
$header = $1;
} elsif (defined $header) {
push @{$h{header}}, $_; }
}
print Dumper \%h;
If your non-header lines can contain :
, you may need something stricter, such as: 如果您的非标题行可以包含:
,则可能需要更严格的内容,例如:
if (/^(Summary|Configuration|Tools):/)
Not sure if you have one file or many, but if the former case: 不确定是否有一个或多个文件,但如果是前一种情况:
Summary: xyz
Configuration: abc 123
Tools: pqr 456
Summary: xyzxyz
Configuration: abcd 1234
Tools: pqr 457
You can use something like the following to print all configuration lines: 您可以使用类似以下的内容来打印所有配置行:
#!/usr/bin/env perl
open (FILE, 'data.txt') or die "$!";
while (<FILE>) {
if (m/^(Configuration):(.*)/) {
print $2 . "\n";
}
}
close FILE;
Change the regex if you want other lines or to m/^(Configuration|Tools|Summary):(.*)/
for all of them. 如果需要其他行,请更改正则表达式;对于所有其他行,请更改为m/^(Configuration|Tools|Summary):(.*)/
。
Or the considerably less elegant 或相当不优雅
#!/usr/bin/perl
my $fi;
my $line;
my @arr;
open($fi, "< tester.txt") or die "$!";
while ($line = <$fi>)
{
if (!($line =~ /:/))
{
push(@arr, $line);
}
}
foreach (@arr)
{
print $_;
}
close ($fi);
close ($fo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.