13

It's Friday afternoon, and my brain has frozen!

grep -E -m 1 -o "<title>(.*)</title>" myfile.rss

returns

<title>Some title</title>

How do I just get Some title?

6
  • 1
    Bash don't have a build-in function to parse XML. You can consider to use PHP, perl to parse XML in proper manner. Then getting the node value will be easy
    – ajreal
    Nov 25, 2011 at 15:07
  • Nah, that's complete overkill for the task!
    – tdc
    Nov 25, 2011 at 15:09
  • 2
    For what I understand we only want to extract title content from xml of known layout, not parse xml. For parsing xml xmlstarlet may be useful utility. Nov 25, 2011 at 15:10
  • There is not perfect way for XML parsing using pure bash commands.
    – ajreal
    Nov 25, 2011 at 15:10
  • 3
    The point is that the OP doesn't need a perfect way. And not talking about bash builtins either. grep is no bash builtin. Nov 25, 2011 at 15:13

1 Answer 1

24

pipe it further through, for instance

sed -e 's,.*<title>\([^<]*\)</title>.*,\1,g'
3
  • Thanks! Brain thawing out ;-)
    – tdc
    Nov 25, 2011 at 15:10
  • +1, but note that using sed to parse XML (or HTML) isn't generally a good idea. It should be done only when the input is well known and doesn't vary unexpectedly. For anything slurped automatically from the internet a proper parser should be used.
    – sorpigal
    Nov 25, 2011 at 16:34
  • @Sorpigal, I agree completely, see comments to the question itself for details. Nov 25, 2011 at 19:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.