Bourne
Bourne

Reputation: 1927

Why doesn't this SED command work?

I am trying to replace all strings in a line which match the pattern .*q= or HTTP with empty string.

I am using the below command for the same.

sed s/.\*q=\|HTTP.*//  

But this doesn't work. It doesn't replace the string with the empty string. Can you please let me know if there is anything wrong with the above command.

Upvotes: 0

Views: 303

Answers (1)

Kent
Kent

Reputation: 195239

this line should work for your requirement:

sed s'/\.\*q=\|HTTP//g'

for example:

kent$  echo "leave.*q=justHTTP foo"|sed s'/\.\*q=\|HTTP//g'        
leavejust foo

your command:

  • better quote expressions in sed command
  • if you want to replace all occurrences of pattern, in your case is .*q or HTTP, you need a g flag in your s/../../
  • if you just want to replace HTTP you should not add .* at the end of it, otherwise it will remove all string after HTTP inclusively.
  • if you want to match literature string .*, you need escape . and *

Upvotes: 3

Related Questions