-1

I'm trying to replace the path line of a file (test.txt) using bash script. The path included in test.txt file is something like this:

setwd("/media/data/myfolder")

And my bash script is:

WORKDIR=$(dirname $(readlink -e $0))
mkdir -p "$WORKDIR/R_results"
outdir="$WORKDIR/R_results"
sed -e 's/^setwd.*/setwd("$outdir")/g' test.txt > test2.txt

When I run it, my ouput is:

setwd("$outdir")

I have tried several combinations with double and single quotes but I can't find the correct way. I hope I've made myself clear.

Thanks!

Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
cucurbit
  • 1,422
  • 1
  • 13
  • 32

2 Answers2

1

Variables are also not expanded inside single quotes, so you need to change your sed to

sed -e "s,^setwd.*,setwd($outdir),g" test.txt > test2.txt

Note, this will fail if $outdir contains commas.

Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
  • If I do: echo "$outdir" /media/R_results This is the path that I want to put inside test.txt file, (replacing /media/data/myfolder). If I run sed -e "s/^setwd.*/setwd($outdir)/g" , I get setwd() as an output. Any other suggestion? – cucurbit Mar 31 '14 at 13:05
  • @user3481031 Odd, can't think of a case that would cause this – Reinstate Monica Please Mar 31 '14 at 13:09
  • Ok, sorry, my fault. When I run your command, I get the following error: sed: -e expression #1, char 21: unknown option to `s And of course the path in the ouput file is not changed. – cucurbit Mar 31 '14 at 13:21
  • if double quote around your file are mandatory `sed -e "s/^setwd.*/setwd(\"$outdir\")/g"` like the source. – NeronLeVelu Mar 31 '14 at 13:53
  • Still having the same error: sed: -e expression #1, char 21: unknown option to `s' I think that is something related to the "/" included in the path.. – cucurbit Mar 31 '14 at 14:06
  • @BroSlow The problem is that I have "/" in my $outdir variable. Because if I remove it, it works perfectly. But the fact is that I need those "/" to have the path obviusly... – cucurbit Mar 31 '14 at 14:25
  • @BroSlow OK the problem is solved. The correct way to do this is the following: sed "s|$OLDPATH|$NEWPATH|g" oldfile >newfile Thanks for your answers anyway :) – cucurbit Mar 31 '14 at 14:30
  • @user3481031 That has the same issue as with a `,`. You can do s'char'$OLDPATH'char'$NEWPATH'char'g, but if the variables contain whatever delimiter you use it will still fail ... should be some better way. – Reinstate Monica Please Mar 31 '14 at 14:33
0

Since the replacement path contains slashes, sed is confused about where the s/// command starts and stops. Use a different character, such as % or Control-A. You also need to use double quotes around the script since you want shell variables to be expanded in it.

WORKDIR=$(dirname $(readlink -e $0))
mkdir -p "$WORKDIR/R_results"
outdir="$WORKDIR/R_results"
sed -e "s%^setwd.*%setwd(\"$outdir\")%g" test.txt > test2.txt

(Using % — or Control-A come to that — assumes that the chosen character won't appear in your file name.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278