0

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.

Billy Grande
  • 417
  • 2
  • 9
  • 19
  • 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. – Stephen P Oct 20 '15 at 03: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). – Billy Grande Oct 20 '15 at 03: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. – Stephen P Oct 20 '15 at 04: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. – Billy Grande Oct 20 '15 at 04:42

2 Answers2

1

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.

tripleee
  • 139,311
  • 24
  • 207
  • 268
  • 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 :) – Billy Grande Oct 20 '15 at 04:48
  • 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? – Billy Grande Oct 20 '15 at 05:12
  • 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. – tripleee Oct 20 '15 at 05:17
  • 1
    F'rinstance http://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables – tripleee Oct 20 '15 at 05:19
  • Awesome! Thanks again! – Billy Grande Oct 20 '15 at 05:26
  • Also http://stackoverflow.com/questions/9329388/sed-and-replace-string-by-a-variable – tripleee Oct 20 '15 at 05:32
  • More like, it's been asked 10,000 times this year alone. – tripleee Oct 20 '15 at 05:37
  • @tripleee as stated using `"` to quote the sed statement means that it will be interpolated and hence may have unintended consequences. If one sticks to using `'` to quote the statement it looks like so: `sed 's/'\''[^'\'']*'\''/'\''replacement'\''/' file`. However one can use back references to be easier on the eye (perhaps only mine) and use: `sed 's/\('\''\)[^\1]*\1/\1replacement\1/' file`. This may only work in GNU sed. – potong Oct 20 '15 at 06:40
  • @potong Since the user wants to also interpolate a variable, double quotes are the way to go here. – tripleee Oct 20 '15 at 07:00
0

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 
ergonaut
  • 6,547
  • 1
  • 14
  • 42
  • 1
    [UU](http://stackoverflow.com/q/11710552/17300)[OC](http://superuser.com/q/323060/101891) — 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. – Stephen P Oct 20 '15 at 03:42