如何grep文件中的特定字符串

How to grep for specific string in a file

这是我的输入文件

<MessageOut>
<Attribute name="Session-Id" value="22250"/><Attribute name="CC-Request-Type" value="2"/><Attribute name="CC-Request-Number" value="1"/><Attribute name="Origin-Host" value="indlnqw291"/><Attribute name="Origin-Realm" value="amdocs.com"/><Attribute name="Auth-Application-Id" value="4"/><Attribute name="Result-Code" value="5031"/><Attribute name="CC-Session-Failover" value="1"/><Attribute name="Low-Balance-Indication" value="0"/><Attribute name="Multiple-Services-Credit-Control"><Group><Attribute name="Result-Code" value="5031"/><Attribute name="Service-Identifier" value="0"/><Attribute name="Rating-Group" value="2"/></Group></Attribute></MessageOut>
<MessageOut>
<Attribute name="Session-Id" value="22250"/><Attribute name="CC-Request-Type" value="3"/><Attribute name="CC-Request-Number" value="2"/><Attribute name="Origin-Host" value="indlnqw291"/><Attribute name="Origin-Realm" value="amdocs.com"/><Attribute name="Auth-Application-Id" value="4"/><Attribute name="Result-Code" value="5031"/></MessageOut>
<MessageOut>
<Attribute name="Session-Id" value="22250"/><Attribute name="CC-Request-Type" value="1"/><Attribute name="CC-Request-Number" value="0"/><Attribute name="Origin-Host" value="indlnqw291"/><Attribute name="Origin-Realm" value="amdocs.com"/><Attribute name="Auth-Application-Id" value="4"/><Attribute name="Result-Code" value="5031"/><Attribute name="CC-Session-Failover" value="1"/><Attribute name="Low-Balance-Indication" value="0"/><Attribute name="Multiple-Services-Credit-Control"><Group><Attribute name="Result-Code" value="5031"/><Attribute name="Service-Identifier" value="0"/><Attribute name="Rating-Group" value="2"/></Group></Attribute></MessageOut>

我想要 "Multiple-Services-Credit-Control"

之后的 grep 结果代码

预期结果:

"CC-Request-Type" value="1"
"CC-Request-Number" value="0"
"Result-Code" value="5031"

"CC-Request-Type" value="2"
"CC-Request-Number" value="1"
"Result-Code" value="5031"

"CC-Request-Type" value="3"
"CC-Request-Number" value="2"
"Result-Code" value="5031"

提前致谢

use strict;
use warnings;

my $filename = 'path_to_input_file\data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";

while (my $row = <$fh>) {
    chomp $row;
    $row =~ /.*?("CC-Request-Type"\svalue="\d*").*?("CC-Request-Number"\svalue="\d*").*?("Result-Code" value="\d*")/;
    if ( (defined ) and (defined ) and (defined )){
        print "\n\n\n\n";
    }
}

这是 Perl 中的解决方案。如果您需要对所用正则表达式的解释,我很乐意解释。

这是XMLIt's a bad idea to try and use regular expressions on XML,因为 XML 是上下文相关的,而正则表达式不是。

使用 XML 解析器。大多数会让你做 xpath,这与正则表达式相当 - 但专门设计用于处理 XML 的上下文性质。

perl 有多个选项。我特别喜欢XML::Twig。:

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;

my $twig = XML::Twig -> parsefile ( 'your_file.xml' );

my @attributes = qw ( CC-Request-Type CC-Request-Number Result-Code );

foreach my $msg ( $twig -> get_xpath('//MessageOut') ) {
    foreach my $attribute ( @attributes ) {
        print "$attribute value=",$msg -> get_xpath("//Attribute[\@name=\'$attribute\']",0)->att('value'),"\n"; 
    }
    print "\n";
}

使用您的样本数据(稍作修改以包含根标签)给出:

CC-Request-Type value=2
CC-Request-Number value=1
Result-Code value=5031

CC-Request-Type value=2
CC-Request-Number value=1
Result-Code value=5031

CC-Request-Type value=2
CC-Request-Number value=1
Result-Code value=5031