1

I am trying to write a script that searches my server in many .app files and outputs the contents between two strings-- only outputting what occurs between the two strings. I am able to do this with AWK and GREP however, the terms I need to search have escape characters so this is not working.

Here is the .dat file excerpt:

   <List Variable name="lookups">
    <Value>
        <Value name="lookups" value="LSM_OPT"/>
        <OTHER GARBAGE... >             
        <Value name="lookup_name" value="123_Done"/>
        <OTHER GARBAGE... >
        <Value name="lookup_name" value="XYZ_DONE"/>
        <OTHER GARBAGE... >         
  </Variable>
  <List Variable name="lookups_for_data">
      <Value>
        <Value name="lookups" value="LSM_OPT"/>
  </Variable>

I need to find all the data between "List Variable name="lookups"' and "/Variable" and nothing else... part of the problem are the escape characters but also, there are other entries in the .dat files that I do not want to capture but they contain my original search value plus some additional data ... e.g. from above " List Variable name="lookups_for_data"".. I do not want these.

I have tried several awk and sed options but I think the problem is the escape characters. Any help will be greatly appreciated! Thanks for your help!!

0

From the sound of it you merely want to find all the content in files that occurs between these two marker strings:

<List Variable name="lookups">
   ...
   ...
</Variable>

To do this you could use this sed command:

$ sed '/<List Variable name="lookups">/,/<\/Variable>/!d;//d' file.dat
    <Value>
        <Value name="lookups" value="LSM_OPT"/>
        <OTHER GARBAGE... >
        <Value name="lookup_name" value="123_Done"/>
        <OTHER GARBAGE... >
        <Value name="lookup_name" value="XYZ_DONE"/>
        <OTHER GARBAGE... >

This prints all the lines in between, but omits the 2 marker lines.

How it works

The above looks for a line that matches your starting pattern, <List Variable name="lookups"> followed by all the lines until it encounters the 2nd matching line, <\/Variable>. For lines in between these, sed will not delete them, !d. For all other lines, the d in !d will delete them. The 2nd //d deletes the marker lines.

If you'd like to do the above, and also print the marker lines, you can change the above sed command to this:

$ sed '/<List Variable name="lookups">/,/<\/Variable>/!d' file.dat
   <List Variable name="lookups">
    <Value>
        <Value name="lookups" value="LSM_OPT"/>
        <OTHER GARBAGE... >
        <Value name="lookup_name" value="123_Done"/>
        <OTHER GARBAGE... >
        <Value name="lookup_name" value="XYZ_DONE"/>
        <OTHER GARBAGE... >
  </Variable>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.