Replace possible phrases with word in sed?

Related searches

I want to be able to replace entire phrases like:

white; phone

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.

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

Using sed to replace words, Is it possible to use sed to replace words, but avoid replacing words that begin with the letters? For example if I replace man with woman, but women becomes� Search for a line that starts with projdir, and replace the whole line with a new one: sed -i 's/^projdir.*$/projdir PacMan/'.ignore ^ and $ are beginning/end-of-line markers, so the pattern will match the whole line;.* matches anything. The -i tells sed to write the changes directly to.ignore, instead of just outputting them

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.

How to Use sed to Find and Replace String in Files, With sed you can search, find and replace, insert, and delete words and lines. It supports basic and extended regular expressions that allow� I need to replace the word false with true only in the pattern "foo": false. The problem is quotes and spaces. The problem is quotes and spaces. I thought about two ways:

This might work for you (GNU sed):

sed '/white/cREJECT' file

Targeted string replacements with sed and AWK, I can't consider every possible replacement case, but I hope the examples I What I'll do is replace the underlined "red" words with "white". The procedure to change the text in files under Linux/Unix using sed: Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt The s is the substitute command of sed for find and replace It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file

sed, a stream editor, If you use sed at all, you will quite likely want to know these commands. # Example: Replace 2nd to 4th lines with the words ' hello ' and ' world ' (-| indicates � With sed you can search, find and replace, insert, and delete words and lines. It supports basic and extended regular expressions that allow you to match complex patterns. In this article, we’ll talk about how to find and replace strings with sed. We’ll also show you how to perform a recursive search and replace. Find and Replace String with sed #

Find and replace text within a file using commands, s = the substitute command; original = a regular expression describing the word to replace (or just AWK, being a text processing utility, is quite appropriate for such task and much more advanced ones based on regular expressions. Another possible way not mentioned here is using the tr command in� If any word appears multiple times in a file then the particular occurrence of the word in each line can be replaced by using `sed` command with the occurrence number. The following `sed` command will replace the second occurrence of the searching pattern in each line of the file, python.txt. $ sed 's/Python/perl/g2' python. txt

Temporarily break out of the single quoted string that is the sed expression, insert "'" (a quoted single quote character) and continue. Repeat for the other quote. $ sed -E 's/^\$\$STATE= ([A-Z] {2}) ([A-Z] {2})/$$STATE='"'"'\1'"'"'/' file $$BATCHCTRL=TEST-012017 $$STATE='CA' $$FROM_DATE=01/10/2017 $$TO_DATE=01/30/2017

  • This question might help :…
  • Say phrases, but in the 4 examples you provided, a pharse coincide with a line, so I'd think that you are actually referring to lines; however you explicitly specify that the phrases are tab delimited too. What do you mean? That you can have multiple phrases, tab-seprated, on a single line? If this is the case, please add an example to cover also this usecase.
  • The Stack Overflow regex tag info page has some hints for beginners.
  • Answers are good but explanations of why? The easy explanation is * on its own is not a regex, you need .*
  • g is not required
  • You are absolutly right :) thanks! I'll edit the answer.
  • This does not work with an input line which does not contain the word phone; the OP says based on whether the word "white" is in the phrase, therefore unless he's worded the question very poorly, this answer does not address his request.