Shell Foo - Right said sed...
Ugh.. this one was a serious PITA.
For as long as I can recall, I have been using the following command
$ sed -i -e 's/foo/bar/g' file.txt
which would swap every occurrence of the word 'foo' with 'bar'.
Today, I was attempting to swap 'MyCertificateARN' with a variable $CERTIFICATEARN
$ sed -i 's/MyCertificateARN/${CERTIFICATEARN}/g' file.txt
sed: -e expression #1, char 69: unknown option to `s'
Huh? So, I tried substituting in other variables as a test, using actual text, swapping the ' with "... nothing was working correctly.
I then echo'd the variable
$ echo ${CERTIFICATEARN}
arn:aws:acm:us-east-1:427832613400:certificate/57c534b7-4560-4610-a084-ad78d906d8df
Wait.. there's a '/' in there. After digging for quite a while, I discovered you can use whatever regex delimiter you want for the sed command.
$ sed -i "s|MyCertificateARN|${CERTIFICATEARN}|g" ${OUTPUT}''
$ grep "cate/" $OUTPUT
"ParameterValue": "arn:aws:acm:us-east-1:427832613400:certificate/57c534b7-4560-4610-a084-ad78d906d8df"
case `sed --version | head -1` in
*GNU*)
# Add the '[' to the beginning of the file
sed -i -e '1i[' ${OUTPUT}
# Remove the trailing ',' from the last entry
sed -i '$ s/},/}/' ${OUTPUT}
# Insert the correct Certificate ARN (use alt regex delimiter for this as ARN contains a /)
sed -i "s|MyCertificateARN|${CERTIFICATEARN}|g" ${OUTPUT}
;;
*)
sed -i'' '1i\
[\
' ${OUTPUT}
;;
esac
echo "]" >> ${OUTPUT}
For as long as I can recall, I have been using the following command
$ sed -i -e 's/foo/bar/g' file.txt
which would swap every occurrence of the word 'foo' with 'bar'.
Today, I was attempting to swap 'MyCertificateARN' with a variable $CERTIFICATEARN
$ sed -i 's/MyCertificateARN/${CERTIFICATEARN}/g' file.txt
sed: -e expression #1, char 69: unknown option to `s'
Huh? So, I tried substituting in other variables as a test, using actual text, swapping the ' with "... nothing was working correctly.
I then echo'd the variable
$ echo ${CERTIFICATEARN}
arn:aws:acm:us-east-1:427832613400:certificate/57c534b7-4560-4610-a084-ad78d906d8df
Wait.. there's a '/' in there. After digging for quite a while, I discovered you can use whatever regex delimiter you want for the sed command.
$ sed -i "s|MyCertificateARN|${CERTIFICATEARN}|g" ${OUTPUT}''
$ grep "cate/" $OUTPUT
"ParameterValue": "arn:aws:acm:us-east-1:427832613400:certificate/57c534b7-4560-4610-a084-ad78d906d8df"
case `sed --version | head -1` in
*GNU*)
# Add the '[' to the beginning of the file
sed -i -e '1i[' ${OUTPUT}
# Remove the trailing ',' from the last entry
sed -i '$ s/},/}/' ${OUTPUT}
# Insert the correct Certificate ARN (use alt regex delimiter for this as ARN contains a /)
sed -i "s|MyCertificateARN|${CERTIFICATEARN}|g" ${OUTPUT}
;;
*)
sed -i'' '1i\
[\
' ${OUTPUT}
;;
esac
echo "]" >> ${OUTPUT}
Comments
Post a Comment