-1

I am just using simple sed substitution to replace some strings in a file by reading its value from a variable

cat file.txt

root="/content/drive/Shared drives/Media Library"
# some other lines
# ...

In the above file txt , I am intending to substitute /content/drive/Shared drives/Media Library in line 1 with the value of a variable path being read through a bash script .

For example - If i enter /home/user as value of variable ,the line 1 in file.txt should be modified as follows :

root="/home/user"

Here's my bash script with the sed expression

#!/usr/bin/env bash

read -e -p "Enter the path: " path

sed -i "1s/\/content\/drive\/Shared drives\/Media Library/${path}/" file.txt

When i execute the bash script and enter path as /home/user , it returns the following error -

sed: -e expression #1, char 52: unknown option to `s'
Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 4
    Use `~` as regex delimiter in sed since `PATH` may contain `/` – anubhava Oct 24 '20 at 11:48
  • 2
    Or preprocess `$path` to escape `/` *and* any other special-to-sed-in-this-context characters it might contain; see [this answer](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29613573#29613573), specifically the section on "Escaping a string literal for use as the *replacement string* in `sed`'s `s///` command". – Gordon Davisson Oct 24 '20 at 12:18
  • @anubhava The path could *also* have `~`; it could have anything you might choose to use as the delimiter. – chepner Oct 24 '20 at 13:15
  • Control characters can also be used as delimiter but one may argue that path may contain those as well :) Anyway `sed` is not right tool for a substitution string coming as input. – anubhava Oct 24 '20 at 13:18
  • The path can contain anything except a null byte, and you can't use that as a `sed` delimiter either (at least, as far as I can tell, though I don't see that necessarily prohibited by the POSIX spec) – chepner Oct 24 '20 at 13:27
  • 1
    @Sachin Given the file you want to edit, you at least need to escape any double quotes that might be part of `$path`, otherwise you'll end up with something like `root="/path/to/"tricky - file"/"`, which will probably confuse whatever is consuming `file.txt`. – chepner Oct 24 '20 at 13:29
  • As anubhava said, `sed` isn't really the right tool when dealing with unknown input. – chepner Oct 24 '20 at 13:30
  • Does this answer your question? [How to pass a variable containing slashes to sed](https://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed) – Wiktor Stribiżew Oct 24 '20 at 21:49

1 Answers1

0

When processing file paths with sed, you can make things easier by using an alternate delimiter that is not a part of the values to be processed. The delimiter can be any character that follows the s command. The forward slash / is often seen in examples, but can be any other character like | or #. This makes it unnecessary to escape the / in the values. For example:

sed -i  "1s|/content/drive/Shared drives/Media Library|${path}|" file.txt
phi ARCHITECT
  • 59
  • 1
  • 3