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 ?
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