2

I can't get the syntax for this command right... I need to change a variable in a file where the variable contains a path.

sessionFolderName=/session_`date '+%y%m%d'`_`date '+%H%M'`

sed "s/sessionFolder=.*/sessionFolder=/"$sessionFolder /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh

However the result is:

sed: -e expression #1, char 35: unknown option to `s'

daniel_e
  • 257
  • 1
  • 11
  • (1) I strongly recommend to use `date '+%y%m%d_%H%M'` to make sure that both the date and the time are atomic. (2) Since the replacement contains a `/` you need to either escape it our use a different delimiter in the sed command: `s#search#replace#` instead of `s/search/replace/` – hek2mgl Aug 24 '17 at 07:14
  • All approaches worked somehow. Escaping was the key here. Didn't get that. Thanks for your help guys! – daniel_e Aug 24 '17 at 12:12

3 Answers3

2

The problem is with the / char in the start of your variable. This interrupts sed syntax:

[root@ ~]# sed "s/sessionFolder=.*/${sessionFolderName}/" text
sed: -e expression #1, char 21: unknown option to `s'
[root@ ~]# echo $sessionFolderName
/session_170824_0942

If you escape it using double backslash - \\, it works:

[root@ ~]# sed "s/sessionFolder=.*/sessionFolder=\\${sessionFolderName}/" text
sessionFolder=/session_170824_0942
Chen A.
  • 10,140
  • 3
  • 42
  • 61
2
# You don't need to (and in your case, should not) invoke date command twice.
# Try running following in bash to see the problem.
#     $ echo $(date '+%s')_$(sleep 1)_$(date '+%s')
# On a different note, it's better to use $(...) instead of backticks.
sessionFolderName="/session_$(date '+%y%m%d_%H%M')"

# You can use several other separators in sed.
#  e.g. :, ;, @, #, _ and even a space
sed "s:sessionFolder=.*:sessionFolder=${sessionFolder}:" /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh

Refer to this regarding using $() instead of backticks

Anubis
  • 6,995
  • 14
  • 56
  • 87
  • Where do you see the `date` command invoked twice? It is invoked once when the variable is set. Referencing the variable later returns the **value** of the variable, not executing the date command again – Chen A. Aug 24 '17 at 09:04
  • @Vinny see how the `sessionFolder` value is set. ``"sessionFolderName=/session_`date '+%y%m%d'`_`date '+%H%M'`"`` – Anubis Aug 24 '17 at 09:15
  • @Vinny see the updated answer. Hope it will clarify things better. – Anubis Aug 24 '17 at 09:26
  • Still, you can add %S to date format and get the seconds value. Then every time you refer $sessionFolderName the value returned is constant, and not calculated on every call.. – Chen A. Aug 24 '17 at 09:29
  • 1
    @Vinny I understand that perfectly. The problem is invoking `date` twice to generate the value is wrong. As it can produce two different values in two invocations. Just run `echo $(date '+%s')_$(sleep 1)_$(date '+%s')` and you'll see. – Anubis Aug 24 '17 at 09:31
  • Oh I just noticed you're right, there are two calls to `date`. I missed it at first glance. – Chen A. Aug 24 '17 at 10:19
  • 1
    You cannot redirect to the same file! You'll loose the file's content. – hek2mgl Aug 24 '17 at 12:30
  • @hek2mgl oops, the same file. I've updated the answer. – Anubis Aug 24 '17 at 13:10
  • Good. You can use `sed -i` to change the file in place – hek2mgl Aug 24 '17 at 13:15
1

1) You need to escape the / in your input

2) modify the format of the sed command as shown below

sessionFolderName=\\/session_`date '+%y%m%d'`_`date '+%H%M'`

sed "s/sessionFolder=.*/sessionFolder="$sessionFolderName"/"  /home/pi/scripts/settings/settings.sh > tmp
Abis
  • 155
  • 8