Two options:
Temporarily break out of the single quoted string that is the sed
expression, insert "'"
(a quoted single quote character) and continue. Repeat for the other quote.
$ sed -E 's/^\$\$STATE=([A-Z]{2}) ([A-Z]{2})/$$STATE='"'"'\1'"'"'/' file
$$BATCHCTRL=TEST-012017
$$STATE='CA'
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017
Note that $
does not have to be escaped in the replacement string.
You could also use \'
in place of "'"
:
$ sed -E 's/^\$\$STATE=([A-Z]{2}) ([A-Z]{2})/$$STATE='\''\1'\''/' file
$$BATCHCTRL=TEST-012017
$$STATE='CA'
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017
or,
Use double quotes around the expression and single quotes as ordinary characters within. This requires that all existing backslashes and $
characters are protected from the shell.
$ sed -E "s/^\\\$\\\$STATE=([A-Z]{2}) ([A-Z]{2})/\$\$STATE='\1'/" file
$$BATCHCTRL=TEST-012017
$$STATE='CA'
$$FROM_DATE=01/10/2017
$$TO_DATE=01/30/2017
The excessive escaping here is needed because you want sed
to get the literal string \$
in the regular expression (and just $
in the replacement part). Within double quotes in the shell, \$
is just a literal $
. \\$
would be a literal backslash followed by $
, and the shell would try to use whatever is after the $
as something to expand ($$
or $STATE
for example). Using \\\$
, we give a literal backslash followed by a literal $
.
I've also removed the g
at the end of the sed
expression. Since you are anchoring it at the beginning of the line, we don't expect the substitution to be performed more than a single time.