Mike
Mike

Reputation: 7914

extracting version string in shell script

I've a progam installed in my linux box called my-scheduler-1.1.0-1112

When I do rpm -qa | grep my, it lists as shown below:

my-scheduler-1.1.0-1112

I want a command which will extract 1.1.0-1112 which is version part in my shell script.

what would be the command to extract it in shell script?

Upvotes: 1

Views: 326

Answers (2)

clt60
clt60

Reputation: 63892

For this question you can try the --queryformat parameter for rpm.

like:

rpm -qa --queryformat '%{NAME}' | grep my

should print

my-scheduler

without the version string... What is much better as mugling it with sed or like. Because you can get something like package-1.0.3-rc2 or soo..

For version-release: use:

--queryformat "%{VERSION}-%{RELEASE}"

and maybe will be useful read http://www.rpm.org/max-rpm/ch-queryformat-tags.html - here is many useful query format tags, so you can directly can get what you want and in what format you want, without scripting...

Upvotes: 3

Jon Lin
Jon Lin

Reputation: 143846

Not sure what other version strings you may encounter, but you can try:

sed -e 's/^[^0-9]*-//g'

This is a sed replace. It's matching the regular expression ^[^0-9]*-, which is:

  • starting from the beginning of the String
  • match as many non-numbers as there are
  • then the very next character is a -

And it replaces everything that matched with a blank, essentially removing it. This should leave everything after that which is the version string.

Upvotes: 1

Related Questions