Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sed to match text and print backward and forward to a marker 2

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
I'd like to find a text match "app1.ear" and print the lines above and below until certain makers/boundaries are found <app-deployment> and </app-deployment>.

Sample search match: app1.ear

Sample Imput Text:
<app-deployment>
<target>AppCluster</target>
<module-type>ear</module-type>
<source-path>deploy/app1.ear</source-path>
<staging-mode>nostage</staging-mode>
</app-deployment>
<app-deployment>
<target>AppCluster</target>
<module-type>ear</module-type>
<source-path>deploy/app2.ear</source-path>
<staging-mode>nostage</staging-mode>
</app-deployment>

Sample output:
<app-deployment>
<target>AppCluster</target>
<module-type>ear</module-type>
<source-path>deploy/app1.ear</source-path>
<staging-mode>nostage</staging-mode>
</app-deployment>

Thanks,

Rob


Rob Jordan
 
One way:
Code:
awk '/<app-deployment>/{x=$0;f=0}/app1.ear/{f=1}{x=x"\n"$0}/<\/app-deployment>/{if(f)print x}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

In the rare case you have XMLStarlet installed, you could do
Code:
xml sel -t -m '//app-deployment/source-path[contains(text(),"app1.ear")]' -c '..' -n /input/file
This would do more accurate lookup than plain text processing. For example PHV's AWK solution ( just like most of the simple text processing solutions we could suggest ) would fail on "app1.ear" appearing anywhere else, for example :
Code:
  [b]<app-deployment>[/b]
    [b]<target>[/b]AppCluster[b]</target>[/b]
    [b]<module-type>[/b]ear[b]</module-type>[/b]
    [b]<source-path>[/b]deploy/app1.ear[b]</source-path>[/b]
    [b]<staging-mode>[/b]nostage[b]</staging-mode>[/b]
  [b]</app-deployment>[/b]
  [b]<app-deployment>[/b]
    [b]<target>[/b]AppCluster[b]</target>[/b]
    [b]<module-type>[/b]ear[b]</module-type>[/b]
    [gray]<!-- depends on [highlight]app1.ear[/highlight], keep this _after_ it -->[/gray]
    [b]<source-path>[/b]deploy/app2.ear[b]</source-path>[/b]
    [b]<staging-mode>[/b]nostage[b]</staging-mode>[/b]
  [b]</app-deployment>[/b]


Feherke.
 
The PHV solution gave me a heads up on something I was attempting to do, so thanks for that and a star.

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top