Previous and Post lines in a pattern

Dear Community;

I am posting this after looking at several solutions that were not fully relevant to the issue that I am facing.

I have a large xml file, 100k+ lines which have patterns like below:

<OfferDefinition Id="11">
        <Type>Account</Type>
        <Description></Description>
               <BreakChargingIntervalsOnDa>0</BreakChargingIntervalsOnDa>
        <ProductCapability>0</ProductCapability>
     <TimeSecondResolutionCapability>0</TimeSecondResolutionCapability>
        <EnableAtProvisioning>0</EnableAtProvisioning>
        <EndOfProvisioning>0</EndOfProvisioning>
        <SelectableInPolicy>0</SelectableInPolicy>
    </OfferDefinition>
    <OfferDefinition Id="12">
        <Type>Timer</Type>
        <Description></Description>  
<StartDateUpdateDuringRemovalProtectionPeriodAllowed>0</StartDateUpdateDuringRemovalProtectionPeriodAllowed>
        <BreakChargingIntervalsOnDa>0</BreakChargingIntervalsOnDa>
        <ProductCapability>0</ProductCapability>
        <TimeSecondResolutionCapability>1</TimeSecondResolutionCapability>
        <EnableAtProvisioning>0</EnableAtProvisioning>
        <EndOfProvisioning>0</EndOfProvisioning>
        <SelectableInPolicy>0</SelectableInPolicy>
    </OfferDefinition>
    <OfferDefinition Id="13">
        <Type>Timer</Type>
        <Description>ATL1 enhancement</Description>
        <StartDateUpdateDuringRemovalProtectionPeriodAllowed>0</StartDateUpdateDuringRemovalProtectionPeriodAllowed>
        <BreakChargingIntervalsOnDa>0</BreakChargingIntervalsOnDa>
        <ProductCapability>0</ProductCapability>
        <TimeSecondResolutionCapability>1</TimeSecondResolutionCapability>
        <EnableAtProvisioning>0</EnableAtProvisioning>
        <EndOfProvisioning>0</EndOfProvisioning>
        <SelectableInPolicy>0</SelectableInPolicy>
    </OfferDefinition>
    <OfferDefinition Id="14">
        <Type>Timer</Type>
        <Description>ATL1 On-net Voice/SMS</Description>
        <MajorPriority>110</MajorPriority>
        <StartDateUpdateDuringRemovalProtectionPeriodAllowed>0</StartDateUpdateDuringRemovalProtectionPeriodAllowed>
        <BreakChargingIntervalsOnDa>0</BreakChargingIntervalsOnDa>
        <ProductCapability>1</ProductCapability>
        <TimeSecondResolutionCapability>1</TimeSecondResolutionCapability>
        <EnableAtProvisioning>0</EnableAtProvisioning>
        <EndOfProvisioning>0</EndOfProvisioning>
        <SelectableInPolicy>0</SelectableInPolicy>
    </OfferDefinition>
    <OfferDefinition Id="15">
        <Type>Timer</Type>
        <Description>ATL1 Data</Description>
        <MajorPriority>130</MajorPriority>
        <StartDateUpdateDuringRemovalProtectionPeriodAllowed>0</StartDateUpdateDuringRemovalProtectionPeriodAllowed>
        <BreakChargingIntervalsOnDa>0</BreakChargingIntervalsOnDa>
        <ProductCapability>1</ProductCapability>
        <TimeSecondResolutionCapability>1</TimeSecondResolutionCapability>
        <EnableAtProvisioning>0</EnableAtProvisioning>
        <EndOfProvisioning>0</EndOfProvisioning>
        <SelectableInPolicy>0</SelectableInPolicy>
    </OfferDefinition>

Each pattern starts with

<OfferDefinition Id="..">

and ends with

</OfferDefinition>

Now within each pattern If

<Type>Timer</Type>

&

<EnableAtProvisioning>0</EnableAtProvisioning>

then I need to return the whole pattern.

I was trying to use grep with -B and -A, but the issue is that the number of lines within each pattern is not fixed.

I also tried using sed with x,p & d ... but was not able to figure out the solution.

Any assistance would be helpful!

Thanks

---------- Post updated at 03:07 PM ---------- Previous update was at 02:51 PM ----------

Just to update, I have the below working command:

sed -n '/<OfferDefinition Id/,/OfferDefinition>/ H;/OfferDefinition>/ {;g;/<Type>Timer/p;s/.*//;x;}' offer_id.txt

However, I want to combine both conditions

<Type>Timer</Type>

&&
Code:

<EnableAtProvisioning>0</EnableAtProvisioning>

In above sed command i am able to use only one condition so far...
BR//

How about extending your approach a little?

sed -n '/<OfferDefinition Id/,/OfferDefinition>/ H;/OfferDefinition>/ {g;/<Type>Timer.*<EnableAtProvisioning/p;s/.*//;x;}' file
1 Like

Another sed solution with the loop technique, written as a multi-liner

sed -n '
  \#<OfferDefinition Id# {
    :Loop
    N
    \#</OfferDefinition># !b Loop
    \#<Type>Timer</Type>.*<EnableAtProvisioning>0</EnableAtProvisioning># p
  }
' file

I have used \#...# rather than the usual /.../ so I do not need ugly \/ escapes in the search string.
Another variant (with default printing if not deleted)

sed '
  \#<OfferDefinition Id# !d
  :Loop
  N
  \#</OfferDefinition># !b Loop
  \#<Type>Timer</Type># !d
  \#<EnableAtProvisioning>0</EnableAtProvisioning># !d
' file
1 Like

Worked like a charm... Thanks once again Rudy!