Javier Salas Javier Salas - 3 months ago 17
Bash Question

How to send parameters on AWK to replace a parameter. Unix Korn Shell

I'm trying to replace a parameter to change a value when the AWK is used to search for a string in a file.

is this possible? I'm doing this.

DisplayMessage()
{
##Parameter 1 = Message ID.
MessageFile="/dev/fs/C/Users/salasfri/Desktop/Messages.txt"
Message=$(awk '$1 ~ /^'$MessageID'$/ {$1=""; print $0}' $MessageFile)
}


the Message File looks for this in the file "MessageFile":

0005 The file ${1} was not tranmitted.


it search for 0005 and get the message "The file ${1} was not tranmitted."

I want to replace ${1} with the name of the file

this could be possible with awk? any idea?

Answer

this should do...

awk '$1~/^'$MessageID'$/ {$1=""; sub("\\${1}",FILENAME); print}' 

but perhaps you want to change to

awk -v mid="${MessageID}" '$1==mid {$1=""; sub("\\${1}",FILENAME); print}' 

since you're looking for an exact match, not pattern match. Also better to use awk variables instead of quote dance.

Comments