I have a list of currently installed kernels, and I'm trying to grep out both the currently installed kernel and the previously installed kernel. For this example, linux-image-3.2.0-60-generic is the currently running kernel, and these are the installed kernels:
linux-image-3.2.0-49-generic
linux-image-3.2.0-51-generic
linux-image-3.2.0-52-generic
linux-image-3.2.0-53-generic
linux-image-3.2.0-54-generic
linux-image-3.2.0-55-generic
linux-image-3.2.0-56-generic
linux-image-3.2.0-57-generic
linux-image-3.2.0-58-generic
linux-image-3.2.0-59-generic
linux-image-3.2.0-60-generic
linux-image-3.2.0-61-generic
linux-image-3.2.0-63-generic
linux-image-3.2.0-64-generic
I can use grep -v $(uname -r)
to remove the currently running kernel from this list. However, I can't seem to use grep -v -B 1 $(uname -r)
to remove current and previous kernel from the list. Is there a way to combine the -B
and -v
? Or am I approaching this entirely from the wrong direction?
-B N
means to printN
lines before the lines that are printed. When used with-v
, it means to print lines before the lines that DON'T match. Which basically means that it willN
lines that match the regexp before each non-matching line.awk
pretty easily. Save the previous line in a variable. When you read a line that doesn't match the current version, print the saved line, and put the current line in the variable. When you read a line that DOES match the current version, clear the variable and don't print anything. Finally, print the saved line in the END block.dpkg -l linux-image-[0-9]* | grep "ii " | grep -oE linux-image-[a-zA-Z0-9.\-]\{0,\}
. This only works on Debian-based systems, of course.