0

I am using sed in a shell script to replace a word in a file with a sentence that is stored in a shell variable. The sentence contains spaces and different characters.

Ex:

sentence="new number is > than 2.5 and < than 3.8"
sed "s|word|$sentence|g" -i Example_file.txt

Line in the file:

tile = "word - Final plot"

expected result:

title = "new number is > than 2.5 and < than 3.8 - Final plot"

obtained result:

title = "new - Final plot"

As you can see above, the problem is that sed always replaces "word" only with the first word of the sentence, it stops at the space. I tried different variations of the command:

sed 's|word|'"$sentence"'|g' -i Example_file.txt
sed "s|word|${sentence}|g" -i Example_file.txt

None of that seems to work. To check if the spaces were indeed the problem I replaced them with "_" , and when I do that, sed replaces using the whole sentence.

Anyone knows how can I force sed to take in the spaces and use the whole sentence?

Inian
  • 80,270
  • 14
  • 142
  • 161
Inacio
  • 3
  • 2
  • 1
    This is very hard to read. Please use the "code formatting" button to format your code/command lines. It's a single click! – Marcus Müller Jul 25 '16 at 10:22
  • Thanks Inian for making the change! – Inacio Jul 25 '16 at 10:30
  • 1
    Are you using `GNU` sed?. Works fine on `GNU sed version 4.2.1` – Inian Jul 25 '16 at 10:31
  • 1
    I am using GNU sed version 4.1.5. If I go to the shell and run the command interactively it does work. However it does not work if I run the script in a non-interactive way. – Inacio Jul 25 '16 at 10:40
  • And you are saying, the same doesn't work from files? – Inian Jul 25 '16 at 10:41
  • I am saying I have a script in a *.sh file and does not work when I run this script. I only get the first word, never the sentence. – Inacio Jul 25 '16 at 10:46
  • Can you try using `sed` expression script? it is working fine for me! `#!/bin/bash sentence="new number is > than 2.5 and < than 3.8" sed -e "s/\/$sentence/" abc.txt` for an input file `$cat abc.txt tile = "word - Final plot" tile = "word1 - Final plot" ` – Inian Jul 25 '16 at 10:56
  • @Inacio: you can add the in-place substitution flag along with it too! Can you see if the above is working? – Inian Jul 25 '16 at 10:57
  • @Inian: yep, it does work for me as well. However it does not work in my script, but I was not yet able to figure it out why. I am running script to run a second script, maybe is that the reason? – Inacio Jul 25 '16 at 12:53
  • @Inacio: Please update the OP with the latest observations! it is not clear from the comments – Inian Jul 25 '16 at 12:58
  • @Inian: My example: file1 (with the sed script) - `#!/bin/sh sed -e "s/\/$1/" -i abc.txt` | file2 (running the sed script) - `#!/bin/sh sentence="new number is > than 2.5 and < than 3.8" ./File1.sh $sentence` – Inacio Jul 25 '16 at 13:06
  • It's extremely unclear what you are trying to show us in that last comment. You show us `file1` containing a sed script which seems to have it's output piped to `file2` containing another shell script which is calling `File1.sh` which may or may not be what you mean by `file1` and contains an unquoted `$sentence` which may or may not be the problem. [edit] your question to show the shell script you are executing, the file you are executing it on, and the command line you are executing. Get rid of the `-i` option from the sed command first though so we can see the output. – Ed Morton Jul 25 '16 at 13:40

1 Answers1

0

Try this, based on fixing what I THINK your comment under your question is telling us you are currently doing:

file1:

#!/bin/sh
sed 's/\<word\>/'"$1"'/' abc.txt

file2:

#!/bin/sh
sentence="new number is > than 2.5 and < than 3.8"
./file1 "$sentence"

Google "quote your shell variables". The above assumes you have GNU sed for word boundaries \<...\>. If you don't then get rid of them to see if it works then we can talk about how you address the need for them in other seds.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    I was indeed not quoting the shell variable. The code also included some variable without space, and as it was working for those I assumed it would also work for the ones with space. Thanks a lot for your answer, it solved my problem. – Inacio Jul 25 '16 at 14:14