Q:

Finding multiline CDATA text in a bunch of XML files (possibly using grep)

I have several thousand (well formed) XML files of the following template :
<?xml version="1.0" ?>
<queries>
  <statement name="foobar">
    <body><![CDATA[
      Several lines
      worth of
      text goes
      in here 
    ]]></body>
  </statement>
  <statement name="whatever">
    [... snip ...]
  </statement>
</queries>

I need to get a list of those statements for which the text content of the body span over 10 lines. Short of writing a python script to do that, is there a simple way to use grep or other standard tools to look into each file and return the statements that span many lines? At the very least, I'd be happy with something that would return a list of filenames for which there is one such statement.

A:

Short of using a real xml library and/or awk/perl/python/ruby this is quite close to what you want (if I understood you right) just using common bash commands. 
Please note that this is really specific to xml files used and should not be encouraged as a general purpose xml parser/splitter.
You'll need output directory for the splitted files. I used /tmp/out for this example:
mkdir -p /tmp/out 

You'll have to clean /tmp/out before each run. Otherwise you'll get result's that don't make sense.
cat /path_to_xml_files/*.xml | \
egrep -v '<?xml version="1.0" \?>|<queries>|</queries>' | \
csplit -q -z - '/statement name/' '{*}' --prefix=/tmp/out/splitout- && \
for x in /tmp/out/splitout-* ; do \
[[ $(wc -l "$x"|cut -d" " -f 1) -gt 10 ]] && \
echo "$x" && \
cat "$x" ; \
done

cat the xml files
Use egrep to remove unwanted lines
split input to multiple files based on your example 'statement name'
loop results
count lines for each file and require it to be more than 10
print output filename 
print output lines

As I said, this is not meant to be a general xml splitter, but should be treated as an example of different shell commands. 
Note: '\' -sign followed by line break means that the line continues without line breaks. 
This just makes it easier to read.

