ChatGPT解决这个技术问题 Extra ChatGPT

sed fails with "unknown option to `s'" error [closed]

sed

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions about general computing hardware and software on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow. Closed 1 year ago. Improve this question

I'm trying to use

sed -i -e "s/.*seb.*/    \"$ftp_login_template\"/" $ftp_dir

however I get this error:

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

I don't understand why since this works perfectly:

sed -i -e "s/.*wbspassword.*/    \"wbspassword\": \"$password\",/" $user_conf

Any ideas as to what I'm doing wrong?

Could this be the problem? ftp_login_template=\${user}:${password}:24:86::\/var\/lib\/clit.${user}\/downloads:\/bin\/false\"

The *BSD error message (including OSX) is "bad flag in substitute command:" -- including it here to make this somewhat more googlable.

E
Elijah Lynn

The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.

Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string:

replacement="/my/path"
sed --expression "s@pattern@$replacement@"

Note that this is not bullet proof: if the replacement string later contains @ it will break for the same reason, and any backslash sequences like \1 will still be interpreted according to sed rules. Using | as a delimiter is also a nice option as it is similar in readability to /.


Just a note to help future users, as I found this answer helpful, as well. The original poster most likely had something in his replacement (most likely hidden in the variable) that contained forward slashes. After the first one of those slashes was encountered, sed considered the replacement terminated, and whatever came next as an option (such as g).
Nicely done (Just remember to wrap variable expressions with " not ' otherwise you'll get a lot of $content in your end result instead of the content of $content :) )
@AbstractDissonance Maybe if you are replacing a password or something weird or random you still have @ in it. You must find a delimiter which is not contained in your substitution string.
@ppumkin just replace "forward slash" delimiters like i.e.: sed -i "s/../../g" with different character i.e: sed -i "s@...@...@g", then the problem of multiple slashes should disappear. And if you use " instead of ' it should also works well with variables inside, i.e.: export var1=bar; sed -i "s@foo@${var1}@g". More info you can find into awesome sed documentation: grymoire.com/Unix/Sed.html
See info sed (source: unix.stackexchange.com/questions/259083/…): "The syntax of the s (as in substitute) command is ‘s/regexp/replacement/flags’. The / characters may be uniformly replaced by any other single character within any given s command."