0

Because picture (or example) is worth more than thousand words, I'll use an example:

RewriteRule ^/products/([0-9]+)$ /content.php?id=$1

In this RewriteRule example we've got simple regular expression. $1 is a reference to something that is captured by ([0-9]+), so it is reference to some number if the matching exists. Is it possible to do something like that in grep?

Let's say, some xml document contains the following :

<someTag>someValue</someTag>

I would like to extract only someValue, but input for second_bash_script for the following:

first_bash_script | grep "<someTag>\([[:digit::]]\)\+</someTag>" | second_bash_script

is someValue. Is it possible to extract only someValue using grep?

Thanks for any clue!

biera
  • 2,608
  • 1
  • 24
  • 26

1 Answers1

1

Those are two separate questions, right?

The answer to the first one would be: use sed, grep doesn't do substitutions.

sed 's_^/products/\([0-9]\+\)_/content.php?id=\1_g'

The second thing can be done with grep using Perl regexp:

$ echo '<someTag>42</someTag>' | grep -oP '(?<=<someTag>)\d+(?=</someTag>)'
42
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • Regex is not the right tool to parse XML. http://stackoverflow.com/a/1732454/223456 – jncraton Jun 04 '12 at 15:22
  • @jncraton Sure, but it doesn't mean one can't extract a numeric value from a single tag without attributes with regex. – Lev Levitsky Jun 04 '12 at 15:37
  • You are quite right. As long as the XML document is fairly simple, this will work just fine. I just wanted to point out there are valid XML documents for which this solution will not work. – jncraton Jun 04 '12 at 15:39
  • @jncraton I'm aware there are better technologies to parse XML, but I really need to do this with regex. Anyway thanks for you attention – biera Jun 04 '12 at 16:09