I am trying to use sed to change a value in a pipe. ------------------- This is the two line script CHANGE="1234" cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt ------------------- and the my_file.txt has: <v1:notificationId>CANCELID</v1:notificationId> it gets changed to $CHANGE instead of the actual value 1234 . I tried putting a \ in front of the $ also and made no difference. What am I not doing correctly. Thanks, jerry
On Tue, Aug 25, 2015 at 1:50 PM, Jerry Geis <geisj at pagestation.com> wrote:> I am trying to use sed to change a value in a pipe. > > ------------------- This is the two line script > CHANGE="1234" > > cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt > ------------------- > > and the my_file.txt has: > <v1:notificationId>CANCELID</v1:notificationId> > > it gets changed to $CHANGE instead of the actual value 1234 . > I tried putting a \ in front of the $ also and made no difference. > > What am I not doing correctly.You need to use double quotes: cat my_file.txt | sed "s/CANCELID/$CHANGE/" > cancel.txt
----- Original Message ----- | I am trying to use sed to change a value in a pipe. | | ------------------- This is the two line script | CHANGE="1234" | | cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt | ------------------- | | and the my_file.txt has: | <v1:notificationId>CANCELID</v1:notificationId> | | it gets changed to $CHANGE instead of the actual value 1234 . | I tried putting a \ in front of the $ also and made no difference. | | What am I not doing correctly. | | Thanks, | | jerry Single quotes = literal. Double quotes = interpreted ;) -- James A. Peltier IT Services - Research Computing Group Simon Fraser University - Burnaby Campus Phone : 604-365-6432 Fax : 778-782-3045 E-Mail : jpeltier at sfu.ca Website : http://www.sfu.ca/itservices Twitter : @sfu_rcg Powering Engagement Through Technology
On 8/25/2015 10:50 AM, Jerry Geis wrote:> ------------------- This is the two line script > CHANGE="1234" > > cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txt > ------------------- > > and the my_file.txt has: > <v1:notificationId>CANCELID</v1:notificationId> > > it gets changed to $CHANGE instead of the actual value 1234 . > I tried putting a \ in front of the $ also and made no difference.use " instead of '. inside ', a $ is just a literal. inside ", $NAME gets substituted. -- john r pierce, recycling bits in santa cruz
On 08/25/2015 10:50 AM, Jerry Geis wrote:> cat my_file.txt | sed 's/CANCELID/$CHANGE/' > cancel.txtsed 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
On 08/25/2015 11:02 AM, Gordon Messmer wrote:> > 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.I sometimes like to use cat purely for stylistic reasons : cat file.txt |\ sed -e s?"foo"?"bar"?g |\ sed -e s?"dirty"?"clean?" |\ > file2.txt It lets me line up my sed expressions. But yes, that is wasting a process for easier visualization. Worth it to me.