XML_GREP(1p)
π NAME π
xml_grep β bestie, it's grep but for XML files, and it hunts down the specific elements you're looking for β¨
This one lives in section 1p of the manual, and it's a User Contributed Perl thing, so you know it's got main character energy π
πΈ SYNOPSYS πΈ
okay so here's the whole vibe, exactly as written:
xml_grep [options] <file list>
or, the other flavor:
xml_grep <xpath expression> <file list>
By default you can just hand xml_grep an XPath expression and a list of files, and it hands you back an XML file with the result. Like, that's it. That's the whole job π
And that shortcut is literally equivalent to writing:
xml_grep --group_by_file file --pretty_print indented --cond <file list>
π OPTIONS π
--helpβ a lil brief help message, just the appetizer π--manβ the full documentation, the whole meal--Versionβ shows you the tool version, no cap--root <cond>β looks for and returns xml chunks matching<cond>
okay so if you use neither --root nor --file, then the element(s) that trigger the --cond option is (are) used instead. And if --cond isn't used at all? Then every element matching the <cond> gets returned π
and yes, you can pass several --roots, stack 'em up π
--cond <cond>β returns the chunks (or file names) only if they contain elements matching<cond>
several --conds can be provided, and in that case they're OR'ed together, bestie π«Ά
--filesβ returns only file names, no XML output generated, we're keeping it minimal β¨
heads up: using this option precludes using any of the options that define the XML output β that's--roots,--encoding,--wrap,--group_by_fileor--pretty_print. Pick a lane, queen.
--countβ returns only the number of matches in each file, the receipts π
same deal: this option precludes using any of the options that define the XML output:--roots,--encoding,--wrap,--group_by_fileor--pretty_print
--strictβ without this option, parsing errors are reported to STDOUT and the file gets skipped. With it? No more casual skipping, we're being strict π--dateβ when on (and it's on by default), the wrapping element gets adateattribute giving the date the tool was run π
with --nodate this attribute is not added, which is lowkey useful if you need to compare 2 runs. Iconic for diffing β¨
--encoding <enc>β the encoding of the xml output, and it'sutf-8by default--nb_results <nb>β outputs only<nb>results, we're capping it--by_fileβ outputs only<nb>results by file, one per file energy π¦--wrap <tag>β wraps the xml result in the tag you provide, and it defaults to'xml_grep'
and if you set wrap to an empty string like --wrap '', then the xml result is not wrapped at all. Naked. Free. Periodt π
--nowrapβ same as using--wrap '': the xml result is not wrapped. That's it, that's the option.--descr <string>β the attributes of the wrap tag, and it defaults toversion="<VERSION>" date="<date>"π--group_by_file <optional_tag>β wraps results for each files into a separate element. By default that element is namedfile, and it has an attribute namedfilenamethat gives the name of the file π
the short version of this option is -g
--exclude <condition>β same as using-vin grep: the elements that match the condition are excluded from the result, and the input file(s) is (are) otherwise unchanged π«
the short form of this option is -v
--pretty_print <optional_style>β pretty prints the output using XML::Twig styles, and"indented","record"or"record_c"are probably what you are looking for π
if the option is used but no style is given, then "indented" is used
short form for this argument is -s
--text_onlyβ Displays the text of the results, one by line. Just the words, no drama π--htmlβ Allow HTML input, files are converted using HTML::TreeBuilder π--Tidyβ Allow HTML input, files are converted using HTML::Tidy π§Ή
π Condition Syntax π
<cond> is an XPath-like expression, as allowed by XML::Twig to trigger handlers. So it's XPath-ish, not full XPath, iykyk π
examples, verbatim, no notes:
'para'
'para[@compact="compact"]'
'*[@urgent]'
'*[@urgent="1"]'
'para[string()="WARNING"]'
see XML::Twig for a more complete description of the <cond> syntax π
oh and also β options are processed by Getopt::Long so they can start with - or -- and can be abbreviated (-r instead of --root for example). Shortcuts, we love her β¨
π DESCRIPTION π
xml_grep does a grep on XML files. But instead of using regular expressions it uses XPath expressions β specifically, the subset of XPath supported by XML::Twig. Not regex, bestie, XPath. Different beast entirely π¦
and the results can be either the names of the files, or the XML elements containing matching elements. Your call π
π«Ά SEE ALSO π«Ά
XML::Twig Getopt::Long
π LICENSE π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Free as in, go off, do your thing π
π AUTHOR π
Michel Rodriguez <mirod@xmltwig.com> β thank you for your service, legend π
okay that's the whole page, bestie!! this one was generated by perl v5.40.1 and it's dated 2025-12-18, so go forth and grep your XML, you absolute icon π
β¨π¦π
π¦ where this page comes from
- ubuntu resolute: xml-twig-tools 1:3.54-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: xml-twig-tools 1:3.54-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms