Grep command on multiple line

Hi All,

I have some xml files and I need to find out all xml where one specific type of pattern is available..

Pattern 1

<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT>without Any Mapping</COMMENT>  
</MAP> 

Pattern 2

<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT></COMMENT>  
</MAP> 

Pattern 3

<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT>Mapp step with mapping</COMMENT>  
  <MAPTARGET>      
  //Import mapping steps
  </MAPTARGET>  
  <MAPSOURCE>  
  //Import mapping steps
  </MAPSOURCE>  
<MAPCOPY FROM="/Package;1;0" TO="/node;1;0">  
</MAPCOPY>  
</MAP>

If you notice inpattern 1 & 2 inside <MAP>tag only <COMMENT> is there but in pattern 3 there is lots of other steps are there. I need to find all xml where its either match pattern 1 or 2.

I have tried with below syntex but its not working

find ./packages -type f -name flow.xml | xargs grep -l '.*<MAP TIMEOUT="[a-zA-Z0-9$]*" MODE="STANDALONE"><COMMENT>"[a-zA-Z0-9!@#$%^&*(),.?":{}|<>\n\[\] ]*"</COMMENT></MAP>'

Please see if anyone can help on this?

Thanks
Baharul Islam

To do what you want you will need

grep -P

which uses the PCRE library, and is found, AFAIK, only by default on some Linux distributions. What EXACT version of UNIX/Linux do you have? What shell?

uname -a

Otherwise you probably should consider awk

Here is a solution which is very useful having seen this concept:

if 'abc' and 'efg' can be on the same line:

grep -zl 'abc.*efg' <your list of files>

if 'abc' and 'efg' must be on different lines:

grep -Pzl '(?s)abc.*\n.*efg' <your list of files>

Hi, try something like:

find ./packages -type f -name flow.xml -exec awk '/^<MAP .*<\/COMMENT>.*<\/MAP>$/{print FILENAME}' RS= {} +

--
or if there can be empty lines in the file, use a dummy record separator to force awk to read the file at once and test for a newline at the end..

find . -type f -name 'infile6584*' -exec awk '/^<MAP .*<\/COMMENT>.*<\/MAP>\n$/{print FILENAME}' RS='�' {} +

You might want to use sed instead. Once you find STANDALONE, you can append the next line and look at it. This is an example of what your sed control file should look like:

/STANDALONE/ {
                                  N
                                  /with mapping/ {
                                   ...insert your code here ...
                                         }
                                   }