익명 06:20

using sed to fix up HTML oversight

using sed to fix up HTML oversight

I'm using the following command to update HTML files

sed -e "s/SRC=\([A-Za-z0-9.\/_-]*\)/SRC=\"\1\" /gi"

I'm having trouble adding + and & to the above as valid filename/path characters. My best understanding is that these special characters need to be escaped, like so:

sed -e "s/SRC=\([A-Za-z0-9.\/_-\+\&]*\)/SRC=\"\1\" /gi"

but I suspect the added bash command line complexity has me stimied. TIA.



Top Answer/Comment:

AFAIK & is only special on the RHS of a sed substitution command (re-substituting the whole matched pattern), and + is only special in extended regular expression (ERE) mode i.e. when using the -E or -r command line options, and then only outside of a bracket expression [ ... ]. Likewise you don't need to escape the / delimiter inside a bracket expression.

Your issue here is likely that - needs to be first or last when used inside a bracket expression - else the expression becomes a character range. So

$ echo 'SRC=dir/foo&bar+baz' | sed -e "s/SRC=\([A-Za-z0-9./_+&-]*\)/SRC=\"\1\" /gi"
SRC="dir/foo&bar+baz"

or (using single quotes to further simplify the quoting)

$ echo 'SRC=dir/foo&bar+baz' | sed -e 's/SRC=\([A-Za-z0-9./_+&-]*\)/SRC="\1" /gi'
SRC="dir/foo&bar+baz"
상단 광고의 [X] 버튼을 누르면 내용이 보입니다