Shell script (not Perl) to parse xml with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script (not Perl) to parse xml with awk
# 1  
Old 03-03-2011
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)
# 2  
Old 03-03-2011
Why not Perl? I'm just curious Smilie
# 3  
Old 03-03-2011
Code:
<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:

Code:
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
Code:
echo "mv $i $n_$d_$u.csv"
echo "mv ${i%.*}_desc.xml $n_$d_$u.xml"

with
Code:
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

Last edited by ctsgnb; 03-03-2011 at 03:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse xml in shell script and extract records with specific condition

Hi I have xml file with multiple records and would like to extract records from xml with specific condition if specific tag is present extract entire row otherwise skip . <logentry revision="21510"> <author>mantest</author> <date>2015-02-27</date> <QC_ID>334566</QC_ID>... (12 Replies)
Discussion started by: madankumar.t@hp
12 Replies

2. Shell Programming and Scripting

How to Parse the XML data along with the URL in Shell Script?

Hi, Can anybody help to solve this. I want to parse some xmldata along with the URL in the Shell. I'm calling the URL via the curl command Given below is my shell script file export... (7 Replies)
Discussion started by: Megala
7 Replies

3. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

4. Shell Programming and Scripting

awk Script to parse a XML tag

I have an XML tag like this: <property name="agent" value="/var/tmp/root/eclipse" /> Is there way using awk that i can get the value from the above tag. So the output should be: /var/tmp/root/eclipse Help will be appreciated. Regards, Adi (6 Replies)
Discussion started by: asirohi
6 Replies

5. Shell Programming and Scripting

AWK to Parse XML messages

Hello Guys, Please help with AWK problem. I have XML file which contains a list of messages for subjects. Example of the messages: , message=, message=, message=, message=, message=, message=, message=, message= I want to use AWK to parse these xml messages but I am... (7 Replies)
Discussion started by: James_Owen
7 Replies

6. Shell Programming and Scripting

Parse XML file in shell script

Hi Everybody, I have an XML file containing some data and i want to extract it, but the specific issue in my file is that the data is repeated some times like the following example : <section1> <subsection1> X=... Y=... Z=... <\subsection1> <subsection2> X=... Y=... Z=...... (2 Replies)
Discussion started by: yassine
2 Replies

7. Shell Programming and Scripting

regex/shell script to Parse through XML Records

Hi All, I have been working on something that doesn't seem to have a clear regex solution and I just wanted to run it by everyone to see if I could get some insight into the method of solving this problem. I have a flat text file that contains billing records for users, however the records... (5 Replies)
Discussion started by: Jerrad
5 Replies

8. Shell Programming and Scripting

Need AWk To parse XML logs

Hi , I need an Awk script to parse my log file . 2008-04-26 10:00:13,391 INFO Logger - <?xml version="1.0" encoding="UTF-8" standalone="no"?><2dm tmsg... (0 Replies)
Discussion started by: amit1_x
0 Replies

9. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question