I want to delete all matches in a text following this pattern:

  • Each match starts with linux-
  • Each match ends with a space
  • (Remove linux-* until immediate next space)

EXAMPLE: From this text:

ibudev1 libudev1 libweather-ion7 libxatracker2 linux-generic linux-headers-generic linux-image-generic linux-libc-dev linux-signed-generic mesa-va-drivers mesa-va-drivers mesa-vdpau-drivers mesa-vdpau-drivers milou

Get this:

ibudev1 libudev1 libweather-ion7 libxatracker2 mesa-va-drivers mesa-va-drivers mesa-vdpau-drivers mesa-vdpau-drivers milou

It is to be run on Ubuntu, so Bash environment.

perl -lpe 's/(?!<\S)linux-\S*(\s+|$)//g'

would do it. That's linux- provided it's not preceded by a non-whitespace ((?!<\S)) followed by any number of non-whitespaces (\S*) followed by one or more whitespaces (\s+) or the end of the line to be replaced with nothing.

The (?!<\S) negative look-behind check is to avoid matching on the linux- found within a package name like in g++-i586-linux-gnu, util-linux-dbgsym...

Note that we remove the whitespace after but not before the package name. That means that if the last word on the line is a linux-* one, there will be trailing whitespace left afterwards.

sed 's/linux-[^ ]* //g'

[^ ]* matches all following characters that are not space and * following that means one or more

  • Thank you very much it worked perfectly! – Smeterlink Mar 25 at 18:28
printf '%s' "$list" | tr ' ' '\n' | grep -v '^linux-' | paste -sd " "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.