Uooo
Uooo

Reputation: 6334

awk: Display matched regex (multiline)

I have a file that looks like this:

SF:/home/developer/project/test/resources/somefile.js
DA:1,2
DA:3,2
end_of_record
SF:/home/developer/project/src/resources/otherfile.js
DA:9,2
DA:15,2
DA:22,2
end_of_record

...some more SF:/home/xxx and end_of_record lines...

The file consists of blocks beginning with SF: ... and ending with end_of_record. Note that the count of the lines inbetween (DA:x,x) can be different. I want to print all blocks that have the String "test" in its first line (like "SF:/home/developer/test/resources/..." here). For this example, my wanted output would be:

SF:/home/developer/project/test/resources/somefile.js
DA:1,2
DA:3,2
end_of_record

I want to do this on a Linux environment.

My first try was to do this using the "sed" command, but after some research it seemed like "awk" is a more suitable tool for doing multiline operations.

Using awk and Regex, this is the command that I have so far:

awk '/SF[:\/a-zA-Z0-9]*test[\/A-Za-z0-9.,:\n]*end_of_record/ {print}' FS="\n" RS="" examplefile

But it outputs the full examplefile, not only the blocks containing "test" in the first line. I am not sure if my Regex is wrong or if I am missing something in my awk call.

How can I get only the block with "test" in the first line?

Upvotes: 0

Views: 2323

Answers (2)

Kent
Kent

Reputation: 195039

you need a flag:

awk '/^SF.*test.*/{f=1}f;/end_of_record/{f=0}' yourFile

Upvotes: 3

Vijay
Vijay

Reputation: 67211

awk '{if($0~/SF:.*\/test\//){P=1;}if($0~/end_of_record/&& P==1){print;P=0;}if(P==1)print}' your_file

Upvotes: 1

Related Questions