Javier Salas Javier Salas -4 years ago 146
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.

##Parameter 1 = Message ID.
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download