extract a number within an xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract a number within an xml file
# 8  
Old 10-06-2008
if you have PHP(or other language that support parsing XML)
Code:
<?php
$seriesname = $argv[1];
$result=file_get_contents("file");
$xml = new SimpleXMLElement($result);
foreach ($xml->Series as $series) {
   if ( strpos($series->SeriesName,$seriesname) !==FALSE ){
        echo $series->seriesid." ";
   }
}
?>

usage:
Code:
#!/bin/bash
seriesname=$1
seriesID=`php5 test.php  $seriesname`
echo $seriesID

# 9  
Old 10-06-2008
Quote:
Originally Posted by tret
Hey vid!

Ok so I'm not sure why but this worked just fine on a mac os x machine but when I used the exact same thing on a linux box it just gives me a blank output, i duplicated everything exactly.

I am running ubuntu hardy, any ideas?

Thanks
Just because this is wrong:

Quote:
Originally Posted by vidyadhar85
not sure try and see
Code:
variable=`sed -ne 's/\(^\<seriesid\>\)\(.*\)\(\<\/seriesid\>\)/\2/p' filename`

The regexp will match any seriesid regardless your condition(s).
Quote:
Originally Posted by vidyadhar85
use head -1 after that command
Code:
variable=`sed -ne 's/\(^\<seriesid\>\)\(.*\)\(\<\/seriesid\>\)/\2/p' filename|head -1`

and @vidyadhar85 use head -1 to print(cheat) the first match? even if it's not the right one.

One solution should be:
Code:
$ v="Young MacGyver"
$ awk -F'[<|>]' -v v="$v" '$2=="seriesid"{s=$3}$2=="SeriesName" && $3==v{print s}' file
83158

.. keep seriesid on hold and if $3 match
# 10  
Old 10-07-2008
Quote:
Originally Posted by danmero
Just because this is wrong:


The regexp will match any seriesid regardless your condition(s).

and @vidyadhar85 use head -1 to print(cheat) the first match? even if it's not the right one.

One solution should be:
Code:
$ v="Young MacGyver"
$ awk -F'[<|>]' -v v="$v" '$2=="seriesid"{s=$3}$2=="SeriesName" && $3==v{print s}' file
83158

.. keep seriesid on hold and if $3 match
Hey danmero this works really well thanks! I played around with it and it only takes the SeriesID if the SeriesName matches. Very cool

Is there any way to modify this so it isn't case sensitve? I have a little trouble with show names such as NCIS or ER or other abbreviated titles that may or may not be in caps.

Thanks again!
# 11  
Old 10-07-2008
Use awk "tolower" function
Code:
awk -F'[<|>]' -v v="$v" '$2=="seriesid"{s=$3}$2=="SeriesName" && tolower($3)==tolower(v){print s}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a value from an xml file

I have this XML file format and all in one line: Fri Dec 23 00:14:52 2016 Logged Message:689|<?xml version="1.0" encoding="UTF-8"?><PORT_RESPONSE><HEADER><ORIGINATOR>XMG</ORIGINATOR><DESTINAT... (16 Replies)
Discussion started by: mrn6430
16 Replies

2. UNIX for Dummies Questions & Answers

Extract Element from XML file

<?xml version = '1.0' encoding =... (8 Replies)
Discussion started by: Siva SQL
8 Replies

3. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

4. Shell Programming and Scripting

Get extract text from xml file

Hi Collegue, i have a file say a.xml. it has contents <bpelFault><faultType>1</faultType><genericSystemFault xmlns=""><part name="payload"><v2:Fault... (10 Replies)
Discussion started by: Jewel
10 Replies

5. Shell Programming and Scripting

extract a pattern from a xml file

Hello All, I want to write a shell script for extracting a content from a xml file the xml file looks like this: <Variable name="moreAxleInfo"> <type> <Table> <type> <NamedType> <type> <TypeRef... (11 Replies)
Discussion started by: suvendu4urs
11 Replies

6. Shell Programming and Scripting

Extract a pattern from xml file

Hi, In a single line I have the below xml content <lst name="responseHeader"><int name="status">0</int><int name="QTime">1</int></lst><lst name="status"><lst name=""><str name="name"/><str name="instanceDir">/var/www/search/current/Search/solr/./</str><str... (5 Replies)
Discussion started by: ashokvpp
5 Replies

7. Shell Programming and Scripting

Extract XML content from a file

310439 2012-01-11 03:44:42,291 INFO PutServlet:? - Content of the Message is:="1.0" encoding="UTF-8"?><ESP_SSIA_ACC_FEED> 310440 <BATCH_ID>12345678519</BATCH_ID> 310441 <UID>3498748823</UID> 310442 <FEED_TYPE>FULL</FEED_TYPE> 310443 <MART_NAME>SSIA_DM_TRANSACTIONS</MART_NAME> 310444... (11 Replies)
Discussion started by: arukuku
11 Replies

8. UNIX for Dummies Questions & Answers

Extract Field Value from XML file

Hi, Within a UNIX shell script I need to extract a value from an XML field. The field will contain different values but will always be 6 digits in length. E.g.: <provider-id>999999</provider-id> I've tried various ways but no luck. Any ideas how I might get the provider id (in this case... (2 Replies)
Discussion started by: pnclayt11
2 Replies

9. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

10. Shell Programming and Scripting

How to extract text from xml file

I have some xml files that got created by exporting a website from RedDot. I would like to extract the cost, course number, description, and meeting information. <?xml version="1.0" encoding="UTF-16" standalone="yes" ?> - <PAG PAG0="3AE6FCFD86D34896A82FCA3B7B76FF90" PAG3="525312"... (3 Replies)
Discussion started by: chrisf
3 Replies
Login or Register to Ask a Question