xgrep man page

xgrep ‐ search content of an XML file

Synopsis

xgrep [-h] [-v] [-t] [-p] [-n nsdef [-n nsdef]...] [-x xpath] [-s string [-s string]...] [-c catalog-file] [infile [infile]...]

Description

xgrep provides facilities for searching content in XML files. The search is specified either as an XPath via the -x flag, or a custom syntax including extended regular expressions via the -s flag. Multiple input files may be specified; if none are provided, input is read from stdin.

Options

xgrep accepts the following options:

-x xpath
An XPath specification of the elements of interest.
-s string
A custom string format specifying the elements of interest. The string format is base-element:element/regex/,element/regex/,... where base-element is the name of the elements within which a match should be attempted, the match succeeding if, for each element/regex/ pair, the content of an element of that name is matched by the corresponding regex. If multiple -s flags are specified, a match by any one of them is returned.
-n nsdef
Define a namespace prefix using the form prefix=namespace.
-c catalog-file
Use the specified catalog file instead of the default.
-p
Regular expressions use Perl syntax as implemented in the PCRE library. (This option only available when xgrep has been linked with the PCRE library.)
-t
Text output mode for generating output formatted for subsequent processing by standard command-line text tools (grep, sed, etc.). Linefeeds are converted to spaces within each matching node set, and each node set is seperated by a newline.
-h
Display usage information
-v
Display version information

Examples

Consider an XML format, for personnel information, in which each person element has descendant elements called name and hiredate. To find all person elements with "Smith" in the content of the name element and "2000" in the content of the hiredate element, do:

xgrep -s 'person:name/Smith/,hiredate/2000/' a.xml b.xml

Using XPath to locate elements with a namespace requires use of the -n flag to define a namespace prefix for use in the XPath. For example to search for "name" elements within a GPX document, do:

xgrep -n gpx="http://www.topografix.com/GPX/1/1" \
      -x '//gpx:name' data.gpx

See Also

regex(7), pcre(3), xmllint(1)

Author

Brendt Wohlberg <software@wohlberg.net>

Referenced By

ksh(1), pmlgrep(1).

9 August 2013