1

Why does regex rpm -qi rpm-build | grep -E [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ return

enter image description here

? Isn't the regex supposed to match periods with \. ? If so why is not the first match the only one returned ?

4
  • Solved with the following sed workaround: rpm -qi rpm-build | grep Version | sed 's/Version\s+:\s+//'
    – Veverke
    Aug 2, 2021 at 12:23
  • 1
    Please, don't post images of text
    – Kusalananda
    Aug 2, 2021 at 13:03
  • @Kusalananda: i wanted to benefit from the colored output. Still want me to remove it and leave a text output instead ?
    – Veverke
    Aug 2, 2021 at 13:47
  • While the red highlighting helps point out the matches, I think it'd be obvious enough to anyone who's paying attention that two of the three lines don't have periods in them, leading to your confusion about trying to match the periods. It would also help any future answerers by giving them copy/paste-able text to test their Answers with (I manually typed in some sample data as a workaround).
    – Jeff Schaller
    Aug 2, 2021 at 18:38

1 Answer 1

2

You haven't quoted the regular expression, so your shell processed the two backslashes. As a result, grep saw this regular expression: [[:digit:]]+.[[:digit:]]+.[[:digit:]]+$ which indeed matches. Quote the expression:

rpm -qi rpm-build | grep -E '[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$'

If you really just want the version information from rpm -q, ask for it directly with --queryformat, and skip the grep:

version=$(rpm -q --queryformat '%{VERSION}' rpm-build)
2
  • Just why is it that without quoting bash interprets the backslash ? It's a bash special character ? And bash here is simply stripping it off, ignoring it ?
    – Veverke
    Aug 2, 2021 at 12:42
  • 1
    That's its job :) gnu.org/savannah-checkouts/gnu/bash/manual/html_node/… -- in case you want to use another special character without its special meaning, such as * or $, etc. The shell doesn't particularly care about plain periods here, so yes, it strips/processes the backslash before handing it all over to grep.
    – Jeff Schaller
    Aug 2, 2021 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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