This is an unexpected result I get
$ dpkg -l | grep linux-modules-nvidia-440-5.4.0-*
rc linux-modules-nvidia-440-5.4.0-26-generic 5.4.0-26.30+2 amd64 Linux kernel nvidia modules for version 5.4.0-26
rc linux-modules-nvidia-440-5.4.0-28-generic 5.4.0-28.32 amd64 Linux kernel nvidia modules for version 5.4.0-28
...
$ dpkg -l | grep linux-modules-extra-5.4.0-*
rc linux-modules-extra-5.4.0-26-generic 5.4.0-26.30 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
rc linux-modules-extra-5.4.0-28-generic 5.4.0-28.32 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
...
$ dpkg -l | grep linux-modules-5.4.0-*
rc linux-modules-5.4.0-26-generic 5.4.0-26.30 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
rc linux-modules-5.4.0-28-generic 5.4.0-28.32 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
...
$ dpkg -l | grep linux-image-5.4.0-*
rc linux-image-5.4.0-26-generic 5.4.0-26.30 amd64 Signed kernel image generic
rc linux-image-5.4.0-28-generic 5.4.0-28.32 amd64 Signed kernel image generic
...
$ dpkg -l | grep linux-*-5.4.0-*
$ dpkg -l | grep linux-*-5.4*
$ dpkg -l | grep linux-*-5\.4*
$ dpkg -l | grep linux-*-5\\.4*
$ dpkg -l | grep "linux-*-5\.4*"
$ dpkg -l | grep "linux-*-5\\.4*"
$ dpkg -l | grep 'linux-*-5\\.4*'
As soon as I add *
prior to .
, grep
stops matching.
I would like to match any of the 4 cases (modules
, etc.)
What is the correct regex so I can still match the results?
Among these, I would like to further filter *5.4.0-[2,3]*
.
But this is what I see as part of the output to $ dpkg -l | grep 5.4.0-[2,3]*
Why would lines with *5.4.0-4*
match? The colorized part is what matches, which is 5.4.0-
in this case, and I do not see how that matches the regex 5.4.0-[2,3]*
.
(I managed to remove those matches with $ dpkg -l | grep 5.4.0-[2,3][[:digit:]]*
, but still I would like to understand why the unexpected matches here).
Or,
How can I combine matching wildcards, text, and multiple strings?
e.g., something like linux-
plus any of modules
, image
, modules-extra
, modules-nvidia-440
, plus -5.4.0-
, plus [2,3]
, plus digit, plus *
.
Note that this is not an XY problem.
The motivation is that I want to use regex to quickly filter a set of package names to remove them all at once.
But in this specific question I am asking why the usage of grep
I tried did not work and what is the correct way of using it.
bash
will try to match files in your current working dir. And then[2,3]*
matches2
,,
or3
, any number of times (including not at all), you may want[23].*
or simply[23]
if you don't care about the rest. – pLumo Oct 20 '20 at 07:51-*
as a regular expression means any number of (or no)-
s, not-
followed by anything as is the case with wildcards. – muru Oct 20 '20 at 07:52