Remove words starting with pattern in bash

I am trying to remove all words starting with -L from a string. Example:

Input:

-L/home/a -la -L/home/b -lb 

Output:

 -la -lb 

Do you know a good and short solution for this. My idea does not work:

 echo $(sed 's/-L\w//g' <<< "-L/home/a -la -L/home/b -lb") 
+4
source share
3 answers

It's close:

 $ echo '-L/home/a -la -L/home/b -lb' | sed 's/-L[^[:space:]]*//g' -la -lb 

Or, if you have no spaces other than spaces, just

 $ echo '-L/home/a -la -L/home/b -lb' | sed 's/-L[^ ]*//g' -la -lb 

You may need to remove extra spaces.

+1
source

You were almost there: slashes are characters other than words, and you need to match multiple charcters, so try:

 echo $(sed 's/-L\S*//g' <<< "-L/home/a -la -L/home/b -lb") 
+1
source

Try this sed

 echo "-L/home/a -la -L/home/b -lb" | sed 's/-L[^ ]*//g' 

If you do not want to have more spaces and spaces in the first line

 sed 's/-L[^ ]*//g;s/^ //' file.txt | tr -s " " 
+1
source

Source: https://habr.com/ru/post/1487843/


All Articles