Is there a problem with using grep in XML?
The problem with grep here is that it’s a generic tool for text processing and it’s not aware of any XML structure. For a very simple scenario, you can get it working. If the document is complex or if you’re using this in a script that will survive months or years and not just a one-off job, you may end up feeling sorry for the results.
How to grep for a URL in a file?
The following fixes your regular expression for this specific case (including numbers and plus-signs): I want to get this URL, assuming it’s the only url in the entire file. This works only if the URL doesn’t have the plus signs. How do I make work for + signs as well?
How to grep for text in a file?
YMMV and GNU grep doesn’t have this flag. That basically says to split the file into chunks delimited by blank lines, then to only print those chunks that match your regular expression. Thanks for contributing an answer to Unix & Linux Stack Exchange!
Can you extract the title of an article with grep?
Extracting the title of the article represented by this document with grep would fail if you used any of the other answers posted here. You could technically write the regular expression to get what you need but it’s a lot easier with XPath.
How to extract a string following a pattern with grep?
The o option makes grep print only the matched text, instead of the whole line. Another way is to use a text editor directly. With Vim, one of the various ways of accomplishing this would be to delete lines without name= and then extract the content from the resulting lines:
How to use grep like regex in Perl?
This option will enable Perl-like regex, allowing you to use \\K which is a shorthand lookbehind. It will reset the match position, so anything before it is zero-width. The o option makes grep print only the matched text, instead of the whole line. Another way is to use a text editor directly.
How to make grep print only the matched text?
If you have an improved version of grep, such as GNU grep, you may have the -P option available. This option will enable Perl-like regex, allowing you to use \\K which is a shorthand lookbehind. It will reset the match position, so anything before it is zero-width. The o option makes grep print only the matched text, instead of the whole line.
When to use grep on a trivial document?
If you know you’re dealing with a trivial document and the format doesn’t change or if it’s a one time job where you can quickly validate the results, you can go for grep as explained by others. Since you already use grep -P, why don’t you use its features?
Is the format indented in an XML file?
With your example XML above (slightly hacked, because it’s not valid, and I’ve assumed that your source XML actually is, and this is a transposition error). NB: This format is indented_a in XML::Twig.