1

How can I get the version number after a .so file using Bash?

I have obtained the library's full path and extracted the name. Like libpcap.so.1.1.1. Now I need to get the version number after so; for example:

libpcap.so.1.1.1 --> 1.1.1
libusb-0.1.so.4.4.4 --> 4.4.4
libpcap.so.1.8.0-PRE-GIT --> 1.8.0

Edit: A sample code using grep can handle the first two examples. But not the third.

grep -Eo '[0-9][^[:alpha:]]+$' <<<'libpcap.so.1.5.3'
4
  • Do you want to scrape a known library's path / filename in order to print the version number or how should the library be identified in first place? – kos Jan 18 '16 at 19:00
  • I have obtained the library full path and extracted the name. Like libpcap.so.1.1.1. Now I need to get the version number after so – ManiAm Jan 18 '16 at 19:04
  • Is the name stored in a file? Are there more than one names? Or is the printed as the result of a command? – kos Jan 18 '16 at 19:14
  • @kos: I guess the question is clear enough and needs no more explanations! I added a sample code. – ManiAm Jan 18 '16 at 19:17
3

I won't guarantee that it will catch every corner case, but how about

grep -Po '\.so\.\K([0-9]+\.)*[0-9]+'
  • match and discard literal .so. on the left (using the PCRE \K modifier)
  • match zero or more occurrences of one or more digits followed by .
  • match one or more digits

Note that we don't want to anchor it to the end of the string ($) as your original expression does, to allow for a trailing string such as -PRE-GIT

1

Using awk:

awk -F'.so.' '{match($2,/([0-9]+\.*)+/);print substr($2, RSTART, RLENGTH)}

Examples

$ awk -F'.so.' '{match($2,/([0-9]+\.*)+/);print substr($2, RSTART, RLENGTH)}' <<<'libpcap.so.1.8.0-PRE-GIT1'
1.8.0

$ awk -F'.so.' '{match($2,/([0-9]+\.*)+/);print substr($2, RSTART, RLENGTH)}' <<<$'libpcap.so.1.1.1\nlibusb-0.1.so.4.4.4\nlibpcap.so.1.8.0-PRE-GIT' 
1.1.1
4.4.4
1.8.0
-1

This works for the examples you provided, and will work as long as the input string follows the pattern .*\.so\.[0-9.]*.*:

grep -Po '.*\.so.\K[0-9.]*' <<<'libpcap.so.1.1.1'

It will match the widest possible sequence of digits / dots after the last .so. substring, if any.

% grep -Po '.*\.so.\K[0-9.]*' <<<$'libpcap.so.1.1.1\nlibusb-0.1.so.4.4.4\nlibpcap.so.1.8.0-PRE-GIT'
1.1.1
4.4.4
1.8.0

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.