Victor M
Victor M

Reputation: 749

Replace possible phrases with word in sed?

I want to be able to replace entire phrases like:

white_phone
white; phone
white.phone
phone;white

with a single phrase "REJECT" based on whether the word "white" is in the phrase. I have tried using:

sed -e 's/*white*/REJECT/g' -e 's/*white/REJECT/g' -e 's/white*/REJECT/g' file

but this only replaces the word "white" and leaves the remainder of the phrase, which I don't want.

The phrases are tab delimited too, if that helps.

Upvotes: 1

Views: 162

Answers (3)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed '/white/cREJECT' file

Upvotes: 0

carrotcakeslayer
carrotcakeslayer

Reputation: 1008

This will give you the output you want, as I understand you neeed. If it is not the case, please show an example of your desired output.

sed -i 's/.*white.*/REJECT/' file

Upvotes: 2

mjuarez
mjuarez

Reputation: 16834

You need to use sed backreferences for this:

sed -e 's/\(^.*\)white.*phone\(.*$\)/\1REJECT\2/g' -e 's/\(^.*\)phone.*white\(.*$\)/\1REJECT\2/g' < file 

That command works for me with the input you provided.

Upvotes: 0

Related Questions