-2

in a script job.sh I have this string:

/home/myname/code_DEM/ > output.txt

that I would like to substitute by:

/home/myname/code_DEM/ > output_"$datetime".txt

where $datetime is a string that automatically loads the date and time.

I tried to do so using the sed command

sed -i 's/"$LINEout"/"$LINEnew"/' job.sh

where $LINEout is the old string that I want to replace by $LINEnew which is the new string (the one with the date and time).

Up to now, I cannot get what I want, I have no change.

PS: if someone has the solution for both Linux and MacOs it would be great: I believe the syntax is slightly different from Linux to Mac for the sed command :)

LeChat
  • 103
  • 3
  • 1
    you are using single quotes so variables are not passed int sed expression. there are so many questions like this out there that it is difficult to imagine that you actually searched before asking. example : http://unix.stackexchange.com/questions/84063/passing-a-variable-to-sed – UnX Nov 17 '16 at 11:55
  • 1
    As @rMistero says, `'s/"$LINEout"/"$LINEnew"/'` will produce this exact string. To expand `$LINEout` and `$LINEnew`, you must not use apostrophes, but rather quotes (see maulingjaws' answer). Also, you do realize that `$LINEout` and `$LINEnew` cannot just contain the literal strings you are replacing, right? You need to escape any occurances of `sed`'s syntax. Suppose you want to replace `/a$` with `b`, then the command you use is `sed 's/\/a\$/b/'`, not `sed 's//a$/b/'`. – Witiko Nov 17 '16 at 11:58
  • use this "s/${LINEout}/${LINEnew}/" – UnX Nov 17 '16 at 12:00
  • Thank you all for your comments. I'm a beginner in script so thanks a lot for your help! – LeChat Nov 17 '16 at 12:13

3 Answers3

0

Try this instead:

sed -i "s|$LINEout|$LINEnew|" job.sh

If under macOS you need to specify a backup, so you would have to use this:

sed -i "" "s|$LINEout|$LINEnew|" job.sh

Examples:

echo "foo
bar" > sample
cat sample 
foo
bar

rep=bar
sed -i.bak "s|$rep|baz|" sample
cat sample
foo
baz
cat sample.bak 
foo
bar

Tested with macOS X 10.11.6 and BSD sed

maulinglawns
  • 8,426
  • 2
  • 28
  • 36
  • ok I just read your answer! Thanks ;) – LeChat Nov 17 '16 at 12:00
  • @LeChat, use another separator than `/`, or better [make sure the variables are in the proper format for `sed`](https://unix.stackexchange.com/questions/129059/how-to-ensure-that-string-interpolated-into-sed-substitution-escapes-all-metac/129063#129063) – Stéphane Chazelas Nov 17 '16 at 12:01
0

Note that BSDs (like Apple OS/X) use sed -i '' ... for in-place editing without backup while GNU uses sed -i .... On most other systems, sed doesn't support in-place editing.

Both got that from perl's -i option. So, for portability, you may want to revert to perl, which will also address problems with special characters in the variable:

A=$LINEout B=$LINEnew perl -i -pe 's/\Q$ENV{A}\E/$ENV{B}/g' job.sh
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
-2

OK found it. Thank you @maulinglawns for your hits ;) In fact the separators need to be change so as:

sed -i "s:$LINEout:$LINEnew:" job.sh
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
LeChat
  • 103
  • 3
  • 1
    you'll need `-i.bkp` to work on both Linux and Mac.. see https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux – Sundeep Nov 17 '16 at 12:00
  • This would point to you having some `/`s in the `$LINEout` and `$LINEnew` variables. See my comment about having to escape the stuff you are passing to `sed`. – Witiko Nov 17 '16 at 12:00