[CentOS] sed question

Gordon Messmer
2015-08-25T18:02:36Z

On 08/25/2015 10:50 AM, Jerry Geis wrote:
> cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt

sed doesn't perform environment variable expansion.  That is to say that
when you instruct sed to substitute "$CHANGE" for
"CANCELID", "$CHANGE"
is a literal string that will be substituted.

bash, on the other hand, does perform environment variable expansion for
strings not enclosed in single quotes.  So, you probably meant:
     cat my_file.txt | sed "s/CANCELID/$CHANGE/" > cancel.txt

In that case, bash will replace $CHANGE with 1234 before starting sed
with that argument.

Additionally, you can avoid using "cat" to make the script more
efficient.  You'll start fewer processes, and complete more quickly.
cat is almost never needed unless you actually need to con"cat"enate
multiple files.

A couple of other changes I'd suggest as better scripting style: Enclose
your variable names in ${} to avoid abiguity, and use lower case
variable names except when you have a variable that you mean to export,
and upper case only exported variables.

     sed "s/CANCELID/${change}/" < my_file.txt > cancel.txt