sed change line error message - sed

I have a config file I need to change (again) and the line is
set wrapper_code=C:\windows\drivers\cache
I need to change it to
set wrapper_code=/home/harry/solo/run
I wrote
cat Proxy.bat | sed -i.bk -e 's/\(^set wrapper_home\=\).*/\/home/'1${dbuser}'/gateway/service\' Proxy.bat
I get an error message
sed: -e expression #1, char 37: unknown option to `s'
What is wrong with my code string

If you are using / as the pattern separator is sed, you have to escape the slashes in the strings (paths). To avoid it, use a different separator:
sed -i.bk -e 's%\^set wrapper_code=C:\\windows\\drivers\\cache%set wrapper_code=/home/harry/solo/run%' Proxy.bat
You also have to escape backslashes, as they have a special meaning in sed.
The cat part is useless.

Related

sed replace two open brackets with a single one

I have a file containing lines like
echo ((hello world))
I want the output to be
echo (hello world)
Any idea how I can accomplish this with sed?
sed 's/echo \(\(/echo \(\/' myfile
sed: -e expression #1, char 21: unterminated `s' command
You accidentally escaped the delimiter:
sed 's/echo \(\(/echo \(\/' myfile
^
I see you're not using sed -E, so it's better thet you drop all slashes as in BRE, brackets and braces shouldn't be escaped, as opposed to ERE. So the correct command would be (either of the two):
sed 's/echo ((/echo (/' myfile
sed -E 's/echo \(\(/echo \(/' myfile

sed: -e expression #1, char XX: unterminated `s' command

I'm trying to use a regular expression with the command sed to substitute any 8 consecutive digits with a specific 8 digits in a whole file. I'm not sure if this is the correct way to do it but I keep getting an error saying the command is unterminated. Any idea why ?
sed -i 's/d\{8\}/20170526' ./somefolder/somefile.xml
the error says char17 which corresponds to the / before 20170526
You need 3 separators (a slash in your case) for a sed switch statement.
sed -eE 's/[0-9]{8}/20170526/' ./somefolder/somefile.xml

SED: unterminated `s' command at hyphen

I'm running the following in my provisioner
sed -i 's/DocumentRoot \/var\/www\/DocumentRoot \/var\/www\/app\/web-root\/\g' /etc/apache2/sites-available/000-default.conf
however I'm getting the error: sed: -e expression #1, char 69: unterminated 's' command - which is a hyphen (-) at that position. I've tried escaping it (\-) to no avail.
Any ideas?
your line:
sed -i 's/DocumentRoot \/var\/www\/DocumentRoot \/var\/www\/app\/web-root\/\g ...
^
sed needs s/.../.../g you have escaped the last / before g flag, more than that, you escaped g flag too. At least this mistake won't let your sed command go.
what better is, you pick another delimiter, if your pattern/replacement containing /(slash) too. It can save those dozens back slashes:
sed -i 's#foo/bar/blah#foo1/bar1/blah1#g` file

Escape backslash character in sed

I need to modify some Windows paths.
For instance,
D:\usr
to
D:\first\usr
So, I have created a variable.
$path = "first\usr"
then used the following command:
sed -i -e 's!\\usr!${path}/g;' test.txt
However, this ends up with the following:
D:\firstSr
How do I escape \u in sed?
Assuming your path variable was assigned properly (without spaces in the assignment: path='first\usr'), fixing step by step for an input file test.txt with one example path:
$ cat test.txt
D:\usr
Your original command
$ sed 's!\\usr!${path}/g;' test.txt
sed: -e expression #1, char 18: unterminated `s' command
doesn't do much, as you've mixed ! and / as the delimiter.
Fixing delimiters:
$ sed 's!\\usr!${path}!g;' test.txt
D:${path}
Now no interpolation happens at all because of the single quotes. I suspect these are just copy-paste mistakes, as you obviously got some output.
Double quotes:
$ sed "s!\\usr!${path}!g" test.txt
bash: !\\usr!${path}!g: event not found
Now this clashes with history expansion. We could escape the !, or use a different delimiter.
/ as delimiter:
$ sed "s/\\usr/${path}/g" test.txt
D:\firstSr
Now we're where the question actually started. ${path} expands to first\usr, but \u has a special meaning in GNU sed in the replacement string: it uppercases the following character, hence the S.
Even without the special meaning, \u would most likely just expand to u and the backslash would be gone.
Escaping the backslash:
$ path='first\\usr'
$ sed "s/\\usr/${path}/g" test.txt
D:\first\usr
This works.
Depending on which shell you are using, you may be able to use parameter expansion to double \ in your substitution string and prevent the \u interpretation:
path="first\usr"
sed -e "s/\\usr/${path//\\/\\\\}/g" <<< "D:\usr"
The syntax for replacing a pattern with the shell parameter expansion is ${parameter/pattern/string} (one replacement) or ${parameter//pattern/string} (replace all matches).
This substitution is not specified by POSIX, but is available in Bash.
Where it is not available, you may need to filter $path through a process:
path=$(echo "$path" | sed 's/[][\\*.%$]/\\&/g')
(N.B. I have also quoted other sed metacharacters in this filter).

About replacing string with sed

I'd like to replace all the \r\n with < br/ >in a document, and I'm trying this see script below
# sed -i 's/\r\n/<br/>' ~/xxd/*
however i got this error back
sed: -e expression #1, char 12: unknown option to `s'
How do i solve this problem?
Thanks!
Your problem is that you have the / separator in your replacement string so sed is assuming that's the end of your replacement, and that the > following it is a flag.
If your sed is modern enough, just use a different separator character, one that's not in the replacement string:
pax$ echo hello | sed -e 's/e/<br />/'
sed: -e expression #1, char 9: unknown option to `s'
pax$ echo hello | sed -e 's?e?<br />?'
h<br />llo
Alternatively, you can escape the offending character but I try to avoid that since it tends to lead to overly sawtooth sed commands like /\/\/\/\/\/\.
The other thing you may want to watch out for is trying to use \n in your regex since sed operates on lines anyway. If your intent is to just strip carriage returns and insert HTML line breaks, then the following sed command may be better:
s?\r$?<br />?