Previous and Post lines in a pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Previous and Post lines in a pattern
# 1  
Old 09-27-2017
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:
Code:
<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
Code:
<OfferDefinition Id="..">

and ends with
Code:
</OfferDefinition>

Now within each pattern If
Code:
<Type>Timer</Type>

&
Code:
<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:

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

However, I want to combine both conditions
Code:
<Type>Timer</Type>

&&
Code:
Code:
<EnableAtProvisioning>0</EnableAtProvisioning>

In above sed command i am able to use only one condition so far...
BR//
# 2  
Old 09-27-2017
How about extending your approach a little?
Code:
sed -n '/<OfferDefinition Id/,/OfferDefinition>/ H;/OfferDefinition>/ {g;/<Type>Timer.*<EnableAtProvisioning/p;s/.*//;x;}' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-27-2017
Another sed solution with the loop technique, written as a multi-liner
Code:
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)
Code:
sed '
  \#<OfferDefinition Id# !d
  :Loop
  N
  \#</OfferDefinition># !b Loop
  \#<Type>Timer</Type># !d
  \#<EnableAtProvisioning>0</EnableAtProvisioning># !d
' file


Last edited by MadeInGermany; 09-27-2017 at 01:45 PM.. Reason: Refined for the requirement in post#1, another variant
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 09-27-2017
Quote:
Originally Posted by RudiC
How about extending your approach a little?
Code:
sed -n '/<OfferDefinition Id/,/OfferDefinition>/ H;/OfferDefinition>/ {g;/<Type>Timer.*<EnableAtProvisioning/p;s/.*//;x;}' file

Worked like a charm... Thanks once again Rudy!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Please remove my previous post

please remove my previous post on "Query on scp" The approach which iam thinking will not work Inconvenience caused regretted (0 Replies)
Discussion started by: nextStep
0 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

5. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

8. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

9. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

10. Shell Programming and Scripting

Moving next 2 lines contents to previous lines

My input file is aaa bbb ccc a1a b1b c1c a2a b2b c2c I want the output file to look like that: aaa,bbb,ccc a1a,b1b,c1c a2a,b2b,c2c How do I achieve this ? (8 Replies)
Discussion started by: Amruta Pitkar
8 Replies
Login or Register to Ask a Question