3

After some googling around I found out that the sed command is all I need. To be honest though, the flags of this command seem impossible to comprehend. All I want is to do is replace a single-quoted string of a file with another one. word1, word2 and so on are not single/double quoted whatsover. Example:

Before: foo.txt

word1 word2 word3 'This the text that needs replacement' word4 word5

After: foo.txt

word1 word2 word3 'I have been replaced' word4 word5

Please note that the text 'This the text that needs replacement' is NOT constant and its content may vary.

4
  • The sed command is so simple that there's not much point in wrapping a script around it. If the text to be replaced is not constant, just type something else between the first two slashes sed -e "s/'something else'/'the new text'/g" foo.txt > bar.txt ... unless this is one step of some common operation, but you'd need to provide more information. Commented Oct 20, 2015 at 3:54
  • @StephenP Thanks for your reply. The syntax seems pretty easy now but I don't know the content of the text so I can't use that 'something else'. In fact, the only constants are the words (wordN). Commented Oct 20, 2015 at 3:58
  • well, how do you know what text will be replaced, and what the replacement text will be? You have to know it at some point in order to run the command. And, will any of the wordM wordN wordO wordP ever contain an apostrophe? That can be important since the single-quote and apostrophe are the same. When programming, you have to be really clear about your problem-space. Commented Oct 20, 2015 at 4:37
  • I know the new text to put, but I don't know the one which will be replaced. None of the word(s) has an apostrophe or any kind of quotes. Commented Oct 20, 2015 at 4:42

2 Answers 2

5

Just replace it with this syntax:

cat foo.txt  | sed -e 's/want to be/have been/g'

to replace the file (eg. on mac)

sed -i '' 's/want to be/have been/g' foo.txt

or other unix

sed -i.bak 's/want to be/have been/g' foo.txt 
Sign up to request clarification or add additional context in comments.

1 Comment

UUOC — sed writes to stdout, so your first example cat foo.txt | ... is equivalent to sed -e 's/want to be/have been/g' foo.txt foo.txt is not modified, it is read by sed, transformed, and written the results written to stdout. Typical use would be sed -e 's/this/that/g' inputfile.txt > outputfile.txt I do use your -i.bak style most of the time myself.
4

This will replace a string within single quotes with ... something. Your question isn't really specific enough.

sed "s/'[^']*'/'something'/" file

Because single quotes are used in the search expression, we put the entire sed script in double quotes instead. This means that anything which will be subject to shell substitution (in particular, backslashes and dollar signs, as well as of course literal double quotes) will need to be escaped in the script expression.

The regular expression '[^']*' means a single quote, followed by a character which isn't a single quote, repeated zero or more times, followed by another single quote. In other words, anything which isn't single quotes, within single quotes.

Also, this will print to standard output. Use sed -i to modify the file in-place, or redirect the output where you want it.

9 Comments

Exactly what I needed! Of course I'm gonna use the -i flag so that the changes take effect. Thanks for the detailed elaboration of your reply :)
I am working on a script which calls the sed command. Instead of something I want to use the value of a string ($str), but it doesn't seem to work. Any ideas?
Sounds like you should write a new question, this time with all the details. Link back to this question for context. This is an extremely common topic, though; you should explain how yours is different from the myriad near-duplicates you will easily find.
Awesome! Thanks again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.