Shell script (not Perl) to parse xml with awk

Hi,

I have to make an script according to these:

  • I have couples of files like:
    xxxxxxxxxxxxx.csv
    xxxxxxxxxxxxx_desc.xml

  • every xml file has diferent fields, but keeps this format:
    ........
    <defaultName>2011-02-25T16:43:43.582Z</defaultName>
    ........
    <reportSearchPath>CAMID("ualdap:u:tekuid=USER,ou=people")/folder[@name='My Folders']/query[@name='TEST']</reportSearchPath>
    .........

  • So I have to make an script that, for every csv&xml couple, parse the xml file and takes date in format yyyymmdd_hhmmss, user (tekuid) and reportname (in the example above, 20110225_164358, USER and TEST). Once these 3 fields are extracted, both csv and xml files have to be renamed to this format:
    reportname_yyyymmdd_hhmmss_user (.csv or .xml)
    (in our example, TEST_20110225_164358_USER)

and moved to /reportname directory and /user directory (one copy in each directory)

My idea is a for loop obtained with a find command, the awk part to extract these 3 fields, and then the rename and move.

Could you please help me? I'm not an expert in scripting, specially in the awk part...

Thx in advance (and sorry for my English)

Why not Perl? I'm just curious :slight_smile:

<defaultName>...</defaultName>
<reportSearchPath>...</reportSearchPath>

...
do these 2 lines appear only once in each and every xml files ?

Here is a ksh shot ok maybe it is not optimized very much because doing 2 pass on the xml file but it should still do the work:

for i in *.csv
do
      d=$(sed '/defaultName>/!d;s/.*\(....-..-..\).\(..:..:..\).*/\1_\2/;s/[-:]//g;q' "${i%.*}_desc.xml")
      sed '/reportSearchPath>/!d;s/.*:tekuid=\([^,]*\),.*@name=\(.\)\(.*\)\2.*/\1 \3/;q' "${i%.*}_desc.xml" | read u n

       echo "mv $i $n_$d_$u.csv"
       echo "mv ${i%.*}_desc.xml $n_$d_$u.xml"
done

Check that the rename command that will be displayed are correct, and if so ,
change

echo "mv $i $n_$d_$u.csv"
echo "mv ${i%.*}_desc.xml $n_$d_$u.xml"

with

mv "$i $n_$d_$u.csv"
mv "${i%.*}_desc.xml $n_$d_$u.xml"

and rerun the script

I didn't treat the "moving to folder" part, since i am not sure to get what you want, however, the code can be adjusted quite easily