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_file or --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_file or --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's utf-8 by 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 to version="<VERSION>" date="<date>") ⭐
  • --group_by_file <optional_tag> β€” wrap results for each file into a separate element. by default that element is named file, and it has an attribute named filename that 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 -v in grep: the elements that match the condition are excluded from the result, and the input file(s) is (are) otherwise unchanged. literally grep -v energy.

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