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-genericlinux-image-3.2.0-51-genericlinux-image-3.2.0-52-genericlinux-image-3.2.0-53-genericlinux-image-3.2.0-54-genericlinux-image-3.2.0-55-genericlinux-image-3.2.0-56-genericlinux-image-3.2.0-57-genericlinux-image-3.2.0-58-genericlinux-image-3.2.0-59-genericlinux-image-3.2.0-60-genericlinux-image-3.2.0-61-genericlinux-image-3.2.0-63-genericlinux-image-3.2.0-64-genericI 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?
How do I combine the -v and -B switches in grep?
grep
You can use 2 grep's to get what you want. The interior grep will generate the list of matches for the current kernel, uname -r and the previous reversion to it. The exterior grep will give you the list of kernels excluding these 2.$ grep -v $(grep -B 1 $(uname -r) list.txt) list.txtExampleTo simulate what you have I've added the contents of these to the file list.txt.$ cat list.txt linux-image-3.2.0-49-genericlinux-image-3.2.0-51-genericlinux-image-3.2.0-52-genericlinux-image-3.2.0-53-genericlinux-image-3.2.0-54-genericlinux-image-3.2.0-55-genericlinux-image-3.2.0-56-genericlinux-image-3.2.0-57-genericlinux-image-3.2.0-58-genericlinux-image-3.2.0-59-genericlinux-image-3.2.0-60-genericlinux-image-3.2.0-61-genericlinux-image-3.2.0-63-genericlinux-image-3.2.0-64-genericNow let's pretend that the command uname -r returns this string:$ uname -r3.2.0-60-genericSo when I run this command I get a list of the kernels minus the uname -r and the version previous to it.$ grep -v $(grep -B 1 3.2.0-60-generic list.txt) list.txt linux-image-3.2.0-49-genericlinux-image-3.2.0-51-genericlinux-image-3.2.0-52-genericlinux-image-3.2.0-53-genericlinux-image-3.2.0-54-genericlinux-image-3.2.0-55-genericlinux-image-3.2.0-56-genericlinux-image-3.2.0-57-genericlinux-image-3.2.0-58-genericlinux-image-3.2.0-61-genericlinux-image-3.2.0-63-genericlinux-image-3.2.0-64-genericThis all assumes that the list of kernels in list.txt is in a sorted format.Additional tipsIf you're interested, I wrote up a A to this Q&A titled: How to programmatically determine the highest version kernel RPM installed? where I show how you can parse out versions of kernels using sort -V.