0

I'm bit confused with sed right now. I have a bash script there is a variable:

_file="/var/log/messages"

and lets say we have file containing:

/var/log/messages
 /var/log/messages
 random  /var/log/messages
random  /var/log/messages
/var/log/messages random
  /var/log/messages random
 random  /var/log/messages/random

I need to match and remove only "/var/log/messages" and "/var/log/messages[[:space:]]"

So the output should be:

 random  
random  
random
  random
 random  /var/log/messages/random

This does not work:

sed -e "s@"$_file"@@" test_file #matches also /var/log/messages/random
sed -e "s@"$_file[[:space:]]"@@" test_file # Does not match end of line occurrences 
VladoPortos
  • 563
  • 5
  • 21

1 Answers1

1

Then match end of line or [[:space:]]. I like single quotes and use double qoutes only for variables.

sed -e 's@'"$_file"'[[:space:]]@@' -e 's@'"$_file"'$@@'

or

sed 's@'"$_file"'\([[:space:]]\|$\)@@'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111