How to print a file section between two regular expressions only if the line inside the section contains a specific line

I have an event file with multiple events with multiple lines between <event>and tags </event>. I want to print the entire From <event>to event </event>only if the line inside this event contains either the string uniqueId = "1279939300.862594_PFM_1_1912320699" or uniqueId = "1281686522.353435_PFM_1_988171542". The file contains 100,000 events, and each event has from 20 to 35 lines (attributes in the event vary in length). I started using sed, but you need a little help:

cat xmlEventLog_2010-03-23T* | sed -nr "/<event eventTimestamp/,/<\/event>/"

What do I need to do to finish this? Also, is sed the best way to do this based on file size?

Thanks in advance

AND

I wanted to change this for an update. For some reason I want to do this with sed. I tried Denis solution, but it does not work:

bash$ grep 1279939300.862594_PFM_1_1912320699 xmlEventLog*
xmlEventLog_2010-03-23T02:41:15_PFM_1_1.xml:    <event eventTimestamp="2010-03-23T02:41:40.861" originalReceivedMessageSize="0" uniqueId="1279939300.862594_PFM_1_1912320699">
bash$ grep 1281686522.353435_PFM_1_988171542 xmlEventLog*
xmlEventLog_2010-03-23T07:47:38_PFM_1_1.xml:    <event eventTimestamp="2010-03-23T08:02:02.299" originalReceivedMessageSize="685" uniqueId="1281686522.353435_PFM_1_988171542">
bash$ time sed -n ':a; /<event>/,/<\/event>/ N; /<event>/,/<\/event>/!b; /<\/event>/ {/uniqueId="1279939300.862594_PFM_1_1912320699"\|uniqueId="1281686522.353435_PFM_1_988171542"/p;d}; ba' xmlEventLog*

real    1m13.134s
user    1m12.463s
sys     0m0.659s
bash$

Which, obviously, did not return anything. So can this be done with sed?

AND

+3
source share
3 answers

Try:

sed -n ':a; /<event>/,/<\/event>/ N; /<event>/,/<\/event>/!b; /<\/event>/ {/uniqueId="1279939300.862594_PFM_1_1912320699"\|uniqueId="1281686522.353435_PFM_1_988171542"/p;d}; ba'
+1
source
awk -vRS="</event>" '/<event>/ && /1279939300.862594_PFM_1_1912320699|1281686522.353435_PFM_1_988171542/{print}' file
+2
source

, |, uniqueid. , , , :

 <event.*?uniqueid=("1279939300\.862594_PFM_1_1912320699"|"1281686522\.353435_PFM_1_988171542").*?</event>
0
source

Source: https://habr.com/ru/post/1761920/


All Articles