XML_GREP(1)
π¦ XML_GREP(1) π¦
okay bestie, so this is xml_grep β a User Contributed Perl Documentation page, and honestly? it's giving "tiny tool, huge slay". it's a grep for XML files and i'm obsessed. let's get into it π
β¨ NAME β¨
xml_grep β grep XML files looking for specific elements
like, you know how grep hunts through text? this does that but for XML elements, which is so much more iconic β¨
πΈ SYNOPSYS πΈ
(no i did not fix the spelling, it's staying, it's canon π)
xml_grep [options] <file list>
or
xml_grep <xpath expression> <file list>
okay so by default you can literally just hand xml_grep an XPath expression and a list of files, and it gives you back an XML file with the result. easy. π
and that's exactly the same as writing:
xml_grep --group_by_file file --pretty_print indented --cond <file list>
periodt. no cap. π«Ά
π OPTIONS π
--helpβ brief help message--manβ the full documentation (the whole thing, the director's cut)--Versionβ shows you the tool version--root <cond>β look for and return xml chunks matching<cond>
lowkey the rules here: if neither --root nor --file are used, then the element(s) that trigger the --cond option is (are) used. and if --cond isn't used either, then all elements matching the <cond> are returned.
you can hand it several --root options, no problem π«Ά
--cond <cond>β only return the chunks (or file names) if they contain elements matching<cond>
several --cond can be provided too β and in that case they're OR'ed together, iykyk π
--filesβ return only file names, no XML output at all
heads up bestie: using this option precludes using any of the options that define the XML output:--roots,--encoding,--wrap,--group_by_fileor--pretty_print. it's one or the other, choose your fighter π
--countβ return only the number of matches in each file
same warning, same energy: using 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 is skipped π (with it? you get to be strict about it. main character behavior.)--dateβ when on (and it is on by default) the wrapping element gets a"date"attribute that gives the date the tool was run. super handy.
with --nodate this attribute is not added β which is genuinely useful if you need to compare 2 runs and don't want the date spoiling the diff π
--encoding <enc>β encoding of the xml output (and it'sutf-8by default)--nb_results <nb>β output only<nb>results. that's it. that's the whole thing. π₯--by_fileβ output only<nb>results by file--wrap <tag>β wrap the xml result in the provided tag (defaults to'xml_grep')
and if you set wrap to an empty string (--wrap '') then the xml result is not wrapped at all. naked xml. bold. π
--nowrapβ same as using--wrap '': the xml result is not wrapped. twins fr fr.--descr <string>β attributes of the wrap tag (defaults toversion="<VERSION>" date="<date>") β--group_by_file <optional_tag>β wrap results for each file into a separate element. by default that element is namedfile, and it has an attribute namedfilenamethat gives the name of the file. bestie this is the organizational slay.
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. literally grep-venergy.
the short form of this option is -v π
--pretty_print <optional_style>β pretty print the output using XML::Twig styles ('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. clean, simple, iconic.--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 basically you're speaking XML::Twig's love language.
examples, bestie, so you can copy-paste and feel powerful:
'para'
'para[@compact="compact"]'
'*[@urgent]'
'*[@urgent="1"]'
'para[string()="WARNING"]'
see XML::Twig for a more complete description of the <cond> syntax π
and real quick: options are processed by Getopt::Long, so they can start with - or -- and can be abbreviated β -r instead of --root, for example. so you can be lazy and it still works, we love that for us π
π₯ DESCRIPTION π₯
xml_grep does a grep on XML files. but instead of using regular expressions (so last season), it uses XPath expressions β well, actually, the subset of XPath supported by XML::Twig. still slay though π
and the results? they can be the names of the files, or XML elements containing matching elements. your choice, your vibe.
π 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. (yes legend, go off, remix it, it's free π«Ά)
β AUTHOR β
Michel Rodriguez <mirod@xmltwig.com> β thank you for this gift, king π
and that's the whole page bestie, no cap, nothing skipped, nothing dropped π
signing off with all the deets: this was perl v5.42.0, dated 2025-01-16, and the page itself is XML_GREP(1) β catch you on the next grep πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-XML-Twig 3.530.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms