Parsing XML using command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing XML using command line
# 8  
Old 07-17-2015
I am very sorry . You are absolutely correct. I was trying to see whats going wrong and realized that my tool gave different XML than actual XML in file on filesystem .

So I tried to ran the sed for this XML set and it is not doing right. Might be we need alternative sed command:

Code:
<?xml version="1.0" encoding="utf-8"?>
<saw:ibot xmlns:saw="com.siebel.analytics.web/report/v1" version="1" priority="normal" jobID="36"><saw:schedule timeZoneId="(GMT-05:00) Eastern Time (US &amp; Canada)" disabled="false"><saw:start repeatMinuteInterval="60" endTime="23:59:00" startImmediately="true"/><saw:recurrence runOnce="false"><saw:weekly weekInterval="1" mon="true" tue="true" wed="true" thu="true" fri="true"/></saw:recurrence></saw:schedule><saw:dataVisibility type="recipient" runAs="cgm"/><saw:choose><saw:when condition="true"><saw:deliveryContent><saw:headline><saw:caption><saw:text>Availability Parity Alert for Next 14 Days </saw:text></saw:caption></saw:headline><saw:conditionalReport/></saw:deliveryContent><saw:postActions/></saw:when><saw:otherwise/></saw:choose><saw:deliveryDestinations><saw:destination category="dashboard"/><saw:destination category="activeDeliveryProfile"/></saw:deliveryDestinations><saw:recipients subscribers="true" customize="false" specificRecipients="false"><saw:subscribers><saw:user name="mbussey@xyz.com" /><saw:user name="kimmy.chan@pqr.com" /><saw:user name="chudgins@gmail.com" /></saw:subscribers></saw:recipients><saw:conditionQuery><saw:reportRefNode path="/shared/Quote/Product/Alerts/Daily Availability Parity Alert - Next 14 Days - Content"/></saw:conditionQuery></saw:ibot>


Last edited by Don Cragun; 07-17-2015 at 11:16 PM.. Reason: Add CODE tags again!
# 9  
Old 07-17-2015
Would that do it?
Code:
 perl -nle '@mail = $_=~/saw:user name="(.*?)"/g and map {print "[email]$_\[/email]"} @mail' infile

This User Gave Thanks to Aia For This Post:
# 10  
Old 07-17-2015
Excellent . Indeed it does. Thank you very much but I output is coming like this :
Code:
[email]chudgins@gmail.com[/email]

So I tried this :
Code:
perl -nle '@mail = $_=~/saw:user name="(.*?)"/g and map {print "$_"} @mail' infile

this works great and removes [email].

Now quetion I have inputfile 1 , 2 ,3 all under dir1 .
So I want perl command to execute across all files under dir1 and give me results in redirected output file. Can we acheive this ?

Last edited by Don Cragun; 07-17-2015 at 11:21 PM.. Reason: Add CODE and ICODE tags again.
# 11  
Old 07-17-2015
Quote:
Originally Posted by pauldx
Hi Experts,

How do I parse a XML with below contents [...]
and retrieve below output ?

Code:
	  [email]mbussey@xyz.com[/email]
	  [email]kimmy.chan@pqr.com[/email]
	  [email]chudgins@gmail.com[/email]

Also I have 5 .xml file with different set of parsing name value. Anyway we can parse and merge them in command line and output in one file ?

thanks and appreciate your help ?
- paul
I thought you wanted the [email][/email] added.

In that case the map is not needed.
Code:
perl -ne '$"=qq{\n}; @mail = $_=~/saw:user name="(.*?)"/g and print "@mail\n"' dir1/inputfile{1,2,3} > output.result

This User Gave Thanks to Aia For This Post:
# 12  
Old 07-17-2015
excellent much better now. thank you

I was trying in loop like belowand this indeed work but yours are much better...
now last and final question is I want unique list of emails in target output file. Anyway can we do that in your previous command ?

Code:
for FILENAME in $(find . -type f )
do
perl -nle '@mail = $_=~/saw:user name="(.*?)"/g and map {print "$_"} @mail' $FILENAME >> /home/orabi/final.txt
done;


Last edited by Don Cragun; 07-17-2015 at 11:58 PM.. Reason: ADD CODE tags again!
# 13  
Old 07-17-2015
Quote:
Originally Posted by pauldx
excellent much better now. thank you

I was trying in loop like belowand this indeed work but yours are much better...
now last and final question is I want unique list of emails in target output file. Anyway can we do that in your previous command ?

for FILENAME in $(find . -type f )
do
perl -nle '@mail = $_=~/saw:user name="(.*?)"/g and map {print "$_"} @mail' $FILENAME >> /home/orabi/final.txt
done;
pauldx, look a little closer to my last post #11, I explained that you do not need to use map, also I gave you a clue as how Perl can take care of reading any files under dir1 without the need to use a for loop.

If you still have issues understanding, please post the result of find . -type f . Make sure you use the proper tags when you post or the moderator, that has edited several time your posts, will get mad.
# 14  
Old 07-18-2015
My apologize. I am new to the forum .
Sorry I mean I want to use your code below . But I need to understand how can I save outputfile using perl command below but need unique email list. I don't want to use |sort | uniq and I am sure we can achieve this in Perl

Code:
perl -ne '$"=qq{\n}; @mail = $_=~/saw:user name="(.*?)"/g and print "@mail\n"' dir1/inputfile{1,2,3} > output.result

Moderator's Comments:
Mod Comment Use CODE tags (not ICODE tags) for full line and multi-line input, output, and code segments.

Last edited by Don Cragun; 07-18-2015 at 12:02 AM.. Reason: Add ICODE tags and change some ICODE tags to CODE tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command Line Perl for parsing fasta file

I would like to take a fasta file formated like >0001 agttcgaggtcagaatt >0002 agttcgag >0003 ggtaacctga and use command line perl to move the all sample gt 8 in length to a new file. the result would be >0001 agttcgaggtcagaatt >0003 ggtaacctga cat ${sample}.fasta | perl -lane... (2 Replies)
Discussion started by: jdilts
2 Replies

2. Shell Programming and Scripting

Suggestions for command line parsing

Hi all I need to put a command line parser together to parse numeric fields and ranges passed to a script. I'm looking for a bash function that is as elegant and simple as possible. So the input would be of the following form - 1,2,8-12 This would return - 1,2,8,9,10,11,12 Input can... (7 Replies)
Discussion started by: steadyonabix
7 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

5. Programming

Parsing command line arguments in Python

Hi, I've a python script called aaa.py and passing an command line option " -a" to the script like, ./aaa.py -a & Inside the script if the -a option is given I do some operation if not something else. code looks like ./aaa.py -a . . if options.a ---some operation--- if not options.a... (1 Reply)
Discussion started by: testin
1 Replies

6. Shell Programming and Scripting

Parsing a command line parameter in script

I have a simple script that builds a complex program call which passes a number of parameters to the program. I'm trying to enhance the script to include the value of the command line parameter in the name of a file being created. The problem I'm having is that the parameter may include a forward... (11 Replies)
Discussion started by: pbmax626
11 Replies

7. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

8. Shell Programming and Scripting

parsing command line switches in Perl

Hi, My perl script takes few switches which i'm parsing through GetOpt::Long module. My script looks like something : myscript.pl --file="foo" --or --file="bar" The --file switch takes 2 arguments foo and bar. The 2 values of file are separated by --or switch. I want to ensure that... (1 Reply)
Discussion started by: obelix
1 Replies

9. UNIX for Dummies Questions & Answers

command line argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies

10. UNIX for Advanced & Expert Users

Parsing the command line arguments

Is there a way to get the command line arguments. I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored... (7 Replies)
Discussion started by: jayakhanna
7 Replies
Login or Register to Ask a Question