I need to extract the name value (Product Finder) from this xml:
File: config.xml
<?xml version="1.0" encoding="utf-8"?>
<widget id="com.abc.app" version="1.3.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0" ios-CFBundleVersion="1.3.1.5" android-versionCode="5">
<name>Product Finder</name>
<description>
Description
</description>
</widget>
I've tried:
mles$ cat config.xml | grep '<name>'
<name>Product Finder</name>
Some other answers suggest using grep -oPm1 "(?<=<xmltag>)[^<]+"
, but that yields an error:
mles$ cat config.xml | grep -oPm1 "(?<=<name>)[^<]+"
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
How can I get the name value? I need a solution without dependencies, so grep
would be preferred
grep
only finds the line, you have to use an additional tool to extract the name, likesed
(not an additional dependency):What
sed
does here is takes everything between<name></name>
and substitutes the whole line with the found text between the tagsYou should use a xml parser, like xmllint for example.
Your xml is invalid and you should fix it, if you can't, use the following regex:
Options:
The following bash built-in will do the job but it's not an xml parser
Your
XML
isn't syntactically right. The W3School XML validitor page says so,Because the header line
<?xml version="1.0" encoding="utf-8"?>
is a processing instruction that identifies the document as beingXML
. All XML documents should begin with anXML
declaration.Also,
xmllint
should be built-into nativeMac OS X
bash by default in which you can just doThe right formatting for your
XML
should have been