我想提取(使用 perl)xxx(Block 之后的字符串:)和 prod(Milestone 之后的字符串:)。字符串(在 Block: 和 Milestone: 之后)和空格数不是标准的。我只能使用底部命令 grep 整行:
use strict;
use warnings;
my $file = 'xxx.txt';
open my $fh, '<', $file or die "Could not open '$file' $!\n";
while (my $line = <$fh>){
chomp $line;
# my @stage_status = $line =~ /(\:.*)\s*$/;
my @stage_status = $line =~ /\b(Block)(\W+)(\w+)/;
foreach my $stage_statuss (@stage_status){
print "$stage_statuss\n";
}
}
文件中的行示例:
| Block: | xxx | Milestone: | prod |
答案 0 :(得分:1)
使用 gnu grep
您可以:
grep -oP '\b(Block|Milestone)\W+\K\w+' file
xxx
prod
RexEx 详细信息:
\b
;词边界(Block|Milestone)
:匹配 Black
或 Milestone
\W+
:匹配 1+ 个非单词字符\K
:重置匹配信息\w+
:匹配 1 个以上的单词字符更新:
根据 OP 编辑的问题建议的 perl
代码:
use strict;
use warnings;
my $file = 'xxx.txt';
open my $fh, '<', $file or die "Could not open '$file' $!\n";
while (my $line = <$fh>){
chomp $line;
print "checking: $line\n";
my @stage_status = $line =~ /\b(?:Block|Milestone)\W+(\w+)/g;
foreach my $stage_statuss (@stage_status){
print "$stage_statuss\n";
}
}
输出:
checking: | Block: | xxx | Milestone: | prod |
xxx
prod
答案 1 :(得分:0)
您可以使用简单的 awk
来完成此操作。通过设置适当的字段分隔符值,我们可以获得所需的值。只需将字段分隔符设置为管道,然后是空格或空格出现,然后在主程序中检查条件,如果第二个字段是块:然后打印第四个字段。
awk -F'\\|[[:space:]]+|[[:space:]]+' '$2=="Block:"{print $4} $6=="Milestone:"{print $8}' Input_file
第二个解决方案: 与我上面的第一个解决方案几乎相同的解决方案,唯一的问题是这里只为 awk
制作 1 个字段分隔符。
awk -F'([[:space:]]+)?\\|([[:space:]]+|$)' '$2=="Block:"{print $3} $4=="Milestone:"{print $5}' Input_file