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 :)
'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 ofsed
's syntax. Suppose you want to replace/a$
withb
, then the command you use issed 's/\/a\$/b/'
, notsed 's//a$/b/'
. – Witiko Nov 17 '16 at 11:58