replace with special characters - sed

I have this in file.txt: DISK: [01/08], I want to replace the 01 with another variable. I want it with wildcard because there are always other numbers
new=05
sed -i "s/DISK: [**/08]/DISK: [$new/08]/" file.txt
error is this
sed: -e expression #1, char 29: unknown option to `s'

Replace the first digit and the following digits with the new number from the variable.
new="42"
sed 's/[0-9]\+/'"$new"'/' file
Output:
DISK: [42/08]
See: The Stack Overflow Regular Expressions FAQ

Related

sed: -e expression #1, char 66: unknown option to s' [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 1 year ago.
My variables issuer and subject contains
issuer=C=IN,ST=TN,L=XYZ,O=ABC,OU=CA,CN=XYZ.com/emailAddress=xyz#gmail
subject=C=GB,ST=London,L=London,O=Global_Security,OU=IT,CN=example.comCA/emailAddress=acl#123
I want to replace a variable leftid and leftca with subject and issuer in xyz.conf respectively.
I'm using sed command like this
sed -i -e '/leftid=/s/=.*/= '$subject'/' /etc/xyz.conf
and
sed -i -e '/leftca=/s/=.*/= '$issuer'/' /etc/xyz.conf
But I'm getting the following error(s)
sed: -e expression #1, char 66: unknown option to s' sed: -e
expression #1, char 83: unknown option tos'
Can anyone help?
Your $issuer variable is expanded in the sed substitution part and so the expression becomes:
sed -i -e '/leftca=/s/=.*/= 'C=IN,ST=TN,L=XYZ,O=ABC,OU=CA,CN=XYZ.com/emailAddress=xyz#gmail'/' /etc/xyz.conf
and this is not a correct sed expression since there is 4 occurrences of the / delimiter.
To sum up your sed command is right just change the s substitution command separator with something not present in your issuer variable, like for instance:
sed -i -e '/leftid=/ s|=.*|= '$subject'|' /etc/xyz.conf

sed: -e expression #1, char 30: unknown option to `s' [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 1 year ago.
I have this as variable
set var=1920 x 1080p / 23.976 fps /16:9 / High Profile 4.1
and i will replace with sed
sed -e "s/1920/%var%/" movie.txt > movie2.txt.nfo
error output is
ed: -e expression #1, char 30: unknown option to `s'
I do not understand what's wrong
Regards
The problem is with slashes. Your final command will have many slashes coming from the variable itself, which will be confused with the / delimiter you provided in the sed command itself.
Luckily, sed can have any char as the delimiter. Change to something like:
sed -e "s-1920-%var%-"
Now, - won't be confused with / (from the variable).

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 change line error message

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.

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 />?