LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-17-2015, 12:59 PM   #1
yozh
Member
 
Registered: Apr 2009
Posts: 32

Rep: Reputation: 0
xpath read value without tag


Hello.

help a noob out please!!

Im doing an small bash script, i need to read the values off an xml file, i had success reading the values with:

xpath xml.file tag

the output of that command is something like this:

<tag>Value that needs to be posted somewhere else</tag>

is there anyway to get the output without the tags???

i been surfing around but i havent found a solution.

thanks in advance.

Last edited by yozh; 07-17-2015 at 01:01 PM. Reason: error in title
 
Old 07-17-2015, 02:21 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Not that I know anything about xpath, but can you pipe the result to sed? Like this:
Code:
echo "<tag>Value that needs to be posted somewhere else</tag>" | sed 's/^<tag>\(.*\)<\/tag>/\1/'
Value that needs to be posted somewhere else
Best regards,
HMW

Last edited by HMW; 07-17-2015 at 02:22 PM. Reason: Clarification
 
Old 07-17-2015, 08:15 PM   #3
yozh
Member
 
Registered: Apr 2009
Posts: 32

Original Poster
Rep: Reputation: 0
Hello.

Thanks for your quick reply i really appreciate it.

the value that im pulling from the xml is called from a variable wich has the xpath command to read it.

x="$(xpath my.xml //tag)"

I dont think i can pipe it to sed in that way just tried:

echo "$x | sed 's/^<tag>\(.*\)<\/tag>/\1/'.
 
Old 07-18-2015, 06:20 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Hello again!

Yes, you can. Your echo statement has errors, take a look at this example:
Code:
XPATH="<tag>Some text parsed from xpath</tag>"
Now the variable $XPATH holds a string with tags just like your example.

You can remove the tags in the variable like this:
Code:
echo $XPATH | sed 's/^<tag>\(.*\)<\/tag>/\1/'
Some text parsed from xpath
You can even assign the new string, without the tags to a new variable "on the fly" if you like:
Code:
NOTAGS=$(echo $XPATH | sed 's/^<tag>\(.*\)<\/tag>/\1/')
echo $NOTAGS 
Some text parsed from xpath
I hope that helped to clear things up.
Best regards,
HMW

Edit:
In your case, you ought to be able to do something like this:
Code:
NOTAGS=$(xpath my.xml //tag | sed 's/^<tag>\(.*\)<\/tag>/\1/')

Last edited by HMW; 07-18-2015 at 06:23 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need assistance with shell script - replace a string with a start tag and end tag SupermanInNY Programming 18 01-02-2010 06:44 PM
Config::Xpath in Ubuntu robertjinx Linux - Server 2 05-13-2009 09:45 AM
Help with XPath please trevelluk Programming 1 11-12-2004 08:45 AM
How to implemtn XPATH irfanhab Programming 1 08-27-2004 11:57 AM
php xpath ??? howto??? amirspl Programming 0 09-20-2003 07:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration