1

For example ls may return string /boot/vmlinuz-5.9.12-100.fc32.x86_64

want a regular expression tool to extract 5.9.12-100

Duplicate of question but additional subject A regex for version number parsing

But because of SO, I can't add my answer...

Stéphane Millien
  • 3,238
  • 22
  • 36
  • parsing linux kernel version strings To parse version number from say "/boot/vmlinuz-15.19.12-100.fc32.x86_64" my answer, although b/c of SO i can't do $ echo "/boot/vmlinuz-15.19.12-100.fc32.x86_64" | \ sed -e 's/\(^[^0-9]*\-\)\([^a-z]*\)\(.*\)/\2/g' -e 's/\.\s*$//g' 15.19.12-100 – user1578821 Mar 27 '21 at 23:54

1 Answers1

4

One solution using uname:

$ uname -r
5.4.0-70-generic
$ uname -r | sed -re 's/(^[0-9]*\.[0-9]*\.[0-9]*-[0-9]*)(.*)/\1/'
5.4.0-70

Another one using ls:

$ ls -1 /boot/vmlinuz* | sed -re 's/(^[a-z/]*-)([0-9]*\.[0-9]*\.[0-9]*-[0-9]*)(.*$)/\2/'
5.4.0-67
5.4.0-70
Stéphane Millien
  • 3,238
  • 22
  • 36