Extract only the Linux Kernel Versions column using AWK

Viewed 143

From the Debian/Ubuntu grub configuration file /boot/grub/grub.cfg, I would like to extract only the Linux Kernel Version columns, using AWK.

I wrote one but very long command using pipes. Can we make it short?.

My command:

# awk -F"--class" '/menuentry/ && /with Linux/ {print $1}' /boot/grub/grub.cfg | awk '{print i++ " : " $5,$6,$7,$8}' | sed -e "s/'/ /g"
0 : Linux 5.12.1
1 : Linux 5.12.1 (recovery mode)
2 : Linux 4.19.0-14-amd64
3 : Linux 4.19.0-14-amd64 (recovery mode)

The above output is the desired output I wanted but I would like to know if the command can be shorter to produce the same output?.

1 Answers

With your shown attempts, could you please try following. This is not tested since actual samples are not given in post.

awk -F"--class" '
/menuentry/ && /with Linux/{
  gsub(/\047/,"",$1)
  split($1,arr," ")
  print i++" : "arr[5],arr[6],arr[7],arr[8]
}
' Input_file

Explanation: Adding detailed explanation for above.

awk -F"--class" '              ##Starting awk program from here, setting field separator as --class for all lines.
/menuentry/ && /with Linux/{   ##Searching menuentry AND with Linux keywords in line.
  gsub(/\047/,"",$1)           ##Globally substituting ' with NULL in 1st field.
  split($1,arr," ")            ##Splitting 1st field into array arr here.
  print i++" : "arr[5],arr[6],arr[7],arr[8]  ##Printing i value(increment with 1 here) then printing values of array 5th, 6th, 7th and 8th elements here.
}
' Input_file                   ##Mentioning Input_file name here.
Related