从两行中提取文本

时间:2018-06-01 07:50:27

标签: linux perl sed

这是我有的文件

Generate random sequence...
Appended bytes 000A - Offset
is 0x30 (collision is not found). OK.

Generate random sequence...
Appended bytes 3725 - Offset
is 0x35 (collision is found). OK.

...

等我需要从Offset开始直到下一个字符串的第一个点开始提取文本。我应该说sed(或perl)它提取所需的文本块?

3 个答案:

答案 0 :(得分:4)

使用sed

sed -nr "/Offset/ {N; s/.*Offset\n([^\.]*\.).*/Offset \1/p}" file

N在模式空间中添加下一行,然后进行正常替换。

答案 1 :(得分:2)

在GNU awk中:

$ awk -v RS="" '{print gensub(/.*(Offset[^.]*\.).*/,"\\1",1)}' file
Offset
is 0x30 (collision is not found).
Offset
is 0x35 (collision is found).

说明:

$ awk -v RS="" '                                # separare records by empty lines
{
    print gensub(/.*(Offset[^.]*\.).*/,"\\1",1) # replace record with what starts
}' file                                         # with Offset up to the first .

答案 2 :(得分:1)

在perl中,您可以尝试这种方式:

my $str = "Generate random sequence...
Appended bytes 000A - Offset
is 0x30 (collision is not found). OK.

Generate random sequence...
Appended bytes 3725 - Offset
is 0x35 (collision is found). OK.
";

while($str=~m/Offset([^\.]+)\./gs) #Upto the first dot we will fetch the text
{ 
    print "Matched: $&\n"; #Matched string will be printed here
}