I am newbie writing a awk script that read dynamically assigned variables from shell, and perform the following logic:
1. if Field1=var1 and Field2=var2, extract Field3
(i)if Field3 contains "%1" or "%2"..."%n" (not determinate), replace with corresponding fields from var3 before output Field3
(ii)if no "%n" found, output Field 3
2. if no match, output var3
I written a partial script to match the fields but have difficulty with the replacing the "%n" porttion.
Any help is much appreciated. Thanks in advance! This is what I have so far.
ie
var1=\"young\"
var2=\"girl\"
var3=\"shells/hawiian sea/shore\"
nawk -F, 'BEGIN {Counter = 0} {if (($1 == tolower('"$var1"')) && ($2 == tolower('"$var2"'")) {print $3; Counter = 1}} END {if (Counter ==0) print '"$var3"'} input_file
Input_file:
young,girl,she sells sea %1 by the %2 shore\r\n
Output:
she sells sea shells by the hawiian sea shore\r\n
1. if Field1=var1 and Field2=var2, extract Field3
(i)if Field3 contains "%1" or "%2"..."%n" (not determinate), replace with corresponding fields from var3 before output Field3
(ii)if no "%n" found, output Field 3
2. if no match, output var3
I written a partial script to match the fields but have difficulty with the replacing the "%n" porttion.
Any help is much appreciated. Thanks in advance! This is what I have so far.
ie
var1=\"young\"
var2=\"girl\"
var3=\"shells/hawiian sea/shore\"
nawk -F, 'BEGIN {Counter = 0} {if (($1 == tolower('"$var1"')) && ($2 == tolower('"$var2"'")) {print $3; Counter = 1}} END {if (Counter ==0) print '"$var3"'} input_file
Input_file:
young,girl,she sells sea %1 by the %2 shore\r\n
Output:
she sells sea shells by the hawiian sea shore\r\n