Remove all matches containing a string until its next space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove all matches containing a string until its next space
# 1  
Old 03-25-2019
Remove all matches containing a string until its next space

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:
Code:
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:
Code:
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.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-25-2019 at 03:27 PM.. Reason: Changed QUOTE to CODE tags.
# 2  
Old 03-25-2019
something along these lines:
Code:
 sed 's/[ ][ ]*linux-[^ ][^ ]*//g' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-26-2019
The previous sample matches and removes leading space.
It's also possible with trailing space. With portable perl one can even include TABs
Code:
perl -pe 's/linux-\S+\s+//g'

Most exact is a "begin anchor" and trailing space becomes optional
Code:
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):
Code:
perl -pe 's/(^|\s)linux-\S+//g'


Last edited by MadeInGermany; 03-27-2019 at 07:22 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 03-26-2019
Quote:
Originally Posted by 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:

Code:
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:

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


I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 04-01-2019
Quote:
Originally Posted by 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:

Code:
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

4. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

5. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

6. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

7. Shell Programming and Scripting

Remove last space in a string?

I have a customer file now and would like to separate the names into two cells in a spreadsheet. Here is my data as an example: SHAWN R KEEGAN shawn r scroggin Shawn Regan Shawn Reilly The first two have the middle initial so I'd like to include them in the "first name" field and the last... (11 Replies)
Discussion started by: Grassy
11 Replies

8. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

9. UNIX for Dummies Questions & Answers

Remove unregconized space from a string

Hi, I need to unload some data from Informix database into text file. But what happen is when i open the text file, some record split into 2 line, become uncomplete. I had checked is due to some unregconized space in particular fields, i used many way to detect this unregconized space, but... (3 Replies)
Discussion started by: Ricole
3 Replies

10. Shell Programming and Scripting

How to remove all matches in a string with sed

if I have "abxcdxefx" and want to remove the x's with sed, how can I do this? Thanks. WHOOPS: Just remembered: echo "abxcdxefx" | sed s/x//g Thanks for reading, though. (0 Replies)
Discussion started by: lumix
0 Replies
Login or Register to Ask a Question