# Remove all matches containing a string until its next space

**URL:** <https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223>\
**Category:** Shell Programming and Scripting\
**Created:** [March 25, 2019, 1:29pm UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223 "2019-03-25T13:29:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tribe](https://community.unix.com/user_avatar/community.unix.com/tribe/32/14254_2.png) [@Tribe](https://community.unix.com/u/Tribe)\
**Post date:** [March 25, 2019, 1:29pm UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223/1 "2019-03-25T13:29:03Z")

</div>

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:

```nohighlight
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:

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

```

It is meant to be run on Ubuntu, so use bash, sed, awk, etc... the linux versions and if possible posix for any system (cygwin, etc) better.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 25, 2019, 1:56pm UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223/2 "2019-03-25T13:56:42Z")

</div>

something along these lines:

```nohighlight
 sed 's/[][]*linux-[^][^]*//g' myFile

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [March 26, 2019, 8:16am UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223/3 "2019-03-26T08:16:06Z")

</div>

The previous sample matches and removes leading space.  
It's also possible with trailing space. With portable perl one can even include TABs

```nohighlight
perl -pe 's/linux-\S+\s+//g'

```

Most exact is a "begin anchor" and trailing space becomes optional

```nohighlight
perl -pe 's/(^|\s)linux-\S+\s*//g'

```

A recent GNU-Linux like Ubuntu can use `sed -r` instead of the `perl -pe` .  
Just seeing the latter sample does not work because the trailing `\s*` consumes the leading space for a subsequent match.  
Because I have no good idea for a fix, here is again one with leading spaces (and also allowing the beginning of the line):

```nohighlight
perl -pe 's/(^|\s)linux-\S+//g'

```

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [March 26, 2019, 3:31pm UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223/4 "2019-03-26T15:31:40Z")

</div>

> [@tribe](#):
>
> 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)

Are you aware that these stipulations will exclude a "linux-something" at the lines end from being removed because there will be no space afterwards? Maybe this is not what you want. Also notice that your clause 2 means that the space immediately following the word "linux-\*" will be removed too.

If your goal is indeed to remove the last match too i'd suggest:

```plaintext
sed 's/linux-[^]* \{0,1\}//g' /path/to/file

```

If you want to retain the blank following the word but would not want to add a space at the end of the string then this:

```plaintext
sed 's/linux-[^]*\( \{0,1\}\)/\1/g' /path/to/file

```

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Tribe](https://community.unix.com/user_avatar/community.unix.com/tribe/32/14254_2.png) [@Tribe](https://community.unix.com/u/Tribe)\
**Post date:** [April 1, 2019, 3:30pm UTC](https://community.unix.com/t/remove-all-matches-containing-a-string-until-its-next-space/375223/5 "2019-04-01T15:30:57Z")

</div>

> [@bakunin](#):
>
> Are you aware that these stipulations will exclude a "linux-something" at the lines end from being removed because there will be no space afterwards? Maybe this is not what you want. Also notice that your clause 2 means that the space immediately following the word "linux-\*" will be removed too.
> 
> If your goal is indeed to remove the last match too i'd suggest:
> 
> ```plaintext
> sed 's/linux-[^]* \{0,1\}//g' /path/to/file
> 
> ```
> 
> If you want to retain the blank following the word but would not want to add a space at the end of the string then this:
> 
> ```plaintext
> sed 's/linux-[^]*\( \{0,1\}\)/\1/g' /path/to/file
> 
> ```
> 
> I hope this helps.
> 
> bakunin

Any of the two work, because the apt command uses space as argument separator, when there are one or more spaces at the end they are ignored and two or more spaces in any place are condensed into one.
