Using multiple gsub() function under a loop in awk

Hi ALL,

I want to replace string occurrence in my file "Config" using a external file named "Mapping" using awk.

$cat Config
! Configuration file for RAVI
! Configuration file for RACHANA
! Configuration file for BALLU

$cat Mapping
ravi:ram
rachana:shyam
ballu:hameed

The expected output after running the awk script should be

! Configuration file for RAM
! Configuration file for SHYAM
! Configuration file for HAMMEED

For the out expected out I used awk as below.

$ awk ' {FS=":"} FNR==NR { array[$1]=$2; next  }  {FS=" "} FNR!=NR 
{ for (i in array)  IGNORECASE = 1 gsub("Configuration file for "i, "Configuration file for " array)    }1
'   Mapping   Config

And the output is coming as expected.
But when I use two gsub() functions (other one for an additional replacement)
one with IGNORECASE = 0 and other with IGNORECASE = 1. my second gsub("Configuration file for "i, "Configuration file for " array[i]) function is not working as expected.

$ awk ' {FS=":"} FNR==NR { array[$1]=$2; next  }  {FS=" "} FNR!=NR 
{ for (i in array)  gsub("^"i, array)   IGNORECASE = 1 gsub("Configuration file for "i, "Configuration file for " array)  }1
'   Mapping   Config

Can any one help me to know how to use two gsub() function under a for loop with first gsub() case sensitive and second one case insensitive search. Can any one help me improving the above code ?

Thanks in advance.....

awk 'BEGIN {FS = ":"}
  NR == FNR {a[$1] = $2; next}
  {for(x in a) {if(toupper($0) ~ toupper(x)) {gsub(toupper(x), toupper(a[x]))}}}1'

To use multiple gsubs, enclose everything in '{}' and separate each with ';'

awk ' {FS=":"} FNR==NR { array[$1]=$2; next  }  {FS=" "} FNR!=NR 
{for(i in array) {gsub("^"i, array); IGNORECASE = 1 gsub("Configuration file for "i, "Configuration file for " array)}}1' Mapping Config
awk 'NR==FNR{$0=toupper($0);split($0,P,":");A[P[1]]=P[2];next} {gsub($NF,A[$NF])}1' Mapping Config

Hello,

Following code may help in same.

awk -F"for|\:" 'NR==FNR{a[" "$1]=toupper($2);next} (tolower($2) in a){print $1 OFS "for" OFS a[tolower($2)]}'  check_get_Mapping check_getConfig

Output will be as follows.

! Configuration file  for RAM
! Configuration file  for SHYAM
! Configuration file  for HAMEED

Thanks,
R. Singh

without gsub

awk -F "[: ]" 'NR==FNR{$0=toupper($0); A[$1]=$2;next} {$NF=A[$NF]}1' Mapping Config

thanks it is working.

just wanted to know what is the use of semicolon ; for multiple gsub() used under same loop. is it mandatory to use ; after each gsub() under a given loop ?

for (i in array)
{
gsub("^"i, array[i]) ;
gsub("Configuration file for "i, "Configuration file for " array[i])
}

moreover my awk cmd is in one line and looks complex , how can i break this to multiple line ? I am using this awk command under a shell script .

#!/usr/bin/sh

fix_config()
{
  if [ -r /etc/Config ] ; then
    awk '{FS=":"} FNR==NR {array[$1]=$2; next} {FS=" "} FNR!=NR {for (i in array) {gsub("^"i, array) gsub("Test "i, "Test "array) ; {if(toupper($0) ~ toupper(i)) {gsub("! Configuration for "toupper(i), "! Configuration for " toupper(array))}}}} !/This is my conf/  1'  /etc/Mapping  /etc/Config  > Config.bk
  fi
}


fix_config

Thanks.........

thanks...