I’m trying to use the AWK in a unix shell script to substitue an instance of one pattern in a file with another and output it to a new file.
Specifically, if the file name is MYFILE.pc, then I’m looking to instances of ‘*MYFILE’ with ‘g_MYFILE’ (without the quotes). For this, I’m using the gsub function in AWK.
I’ve successfully got the output file written out and all instances replaced as required, however, the script is also replacing instances of ‘MYFILE’ (i.e. without the star) with ‘g_MYFILE’
Here is the script:
awk -v MODNAM=${OUTPUT_FILE%.pc} '
{
gsub("\*"MODNAM, "g_" MODNAM);
print
}' ${INPUT_FILE} > ${FULL_OUTPUT_FILENAME}
To clarify the script performs the following conversion:
- ‘*MYFILE’ –> ‘g_MYFILE’
- ‘MYFILE’ –> ‘g_MYFILE’
I only want the first conversion to be performed. Does anyone have any suggestions?
You may need to double escape the
*because you are using a dynamic regexp instead of a regexp constant as the first argument togsub. See section 3.8 of the GAWK manual for more information.