user9171470
user9171470

Reputation:

Extract text from two lines

Here a file which I have

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.

...

and etc. I need extract text started since Offset till first dot on the next string. What I should to say sed (or perl) that it extract required text block?

Upvotes: 0

Views: 100

Answers (3)

James Brown
James Brown

Reputation: 37454

In GNU awk:

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

Explained:

$ 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 .

Upvotes: 2

Hazzard17
Hazzard17

Reputation: 723

With sed:

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

N adds next line to pattern space, then do normal substitution.

Upvotes: 4

ssr1012
ssr1012

Reputation: 2589

In perl you can try this way:

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
}

Upvotes: 1

Related Questions