【发布时间】:2015-09-07 11:31:58
【问题描述】:
我正在匹配一个文件中的字符串,并希望打印在某个文件中该字符串之后写入的值。这是我尝试过的代码。它运行良好,但没有产生任何输出
use strict;
use warnings;
open(my $file, "<", "abc.txt") || die ("Cannot open file.\n");
open(my $out, ">", "output.txt") || die ("Cannot open file.\n");
while(my $line =<$file>) {
chomp $line;
if ($line =~ /xh = (\d+)/) {
print $out $_;
}
}
abc.txt
a = 1 b = 2 c = 3 d = 4
+xh = 10 e = 9 f = 11
+some lines
+xh = 12 g=14
+some lines
some lines
+xh = 13 i=15 j=20
some lines
output.txt
10
12
13
请建议改进我的代码。每个 xh 前都有一个“+”号,每个“=”号前后都有一个空格。我需要在其他文件中打印 xh 的每个值。在几行的开头有一个“+”号。提前致谢。
【问题讨论】:
标签: perl