Search code examples
shellawkescapingquoting

awk: Escaping and quoting not working on string "...$"


Below is my sample data, in a file called 'tt':

"UDBPEM1 "."HISTOGRAMBIN_@@@@@002__UDBDI02$" 
"UDBPEM1 "."HISTOGRAMBIN_@@@@@002__UDBDI02$"

I want to replace "HISTOGRAMBIN_@@@@@002__UDBDI02$" with "HISTOGRAMBIN_@@@@@002__UDBDI02$_MIG"

Why does this awk command intended to do a substitution not work?

awk '/HISTOGRAMBIN_@@@@@002__UDBDI02\$/ {sub("'HISTOGRAMBIN_@@@@@002__UDBDI02\$'","'HISTOGRAMBIN_@@@@@002__UDBDI02\$_MIG'")} {print}' tt

EDIT: The awk command here is used to replace the object name picked from another file. $ can be anywhere in the name, but i want _mig to be at the end. My actual awk command which i use in my script is below:

awk '/"'${TAB_NAME}'"/{if (M==""){sub("'${TAB_NAME}'","'${TAB_NAME}_${TAB_EXT}'");M=1}}{print}'  filename

I have to use "" to expand awk to use shell variables and I just want to replace the first occurrence.


Solution

  • in your awk line, the quotes were not correctly used. do

    awk '{sub(/H....\$/, "replacement")}7' file