Search and replace with awk

Hi Shell Tigers,
I am trying to acheive search and replace strings within a setup file.
Help much appreciated.

  test.setup
    ORACLE_HOME=/oracle/product/11.0.0/home01
    PATH1=/perm_loc/3222/FTP/cfg1
    PATH2=/perm_loc/3222/FTP/cfg2/bin
    PATH3=/perm_loc/3222/FTP/cfg3/bin

So far I have tried

  cat test.setup | awk -F= -v a="MonsterHouse.Scully" -v b="Mickey/Minie" '{if ($1 == "ORACLE_HOME" ) printf "ORACLE_HOME=%s\n",a;
  else if ( $2 ~ /perm_loc/ ) print (gsub("perm_loc",b)); else print;}'

I get the following results
test.setup

 ORACLE_HOME=MonsterHouse.Scully 
 1
 1 
 1

Expected :
test.setup

    test.setup
    ORACLE_HOME=MonsterHouse.Scully
    PATH1=/Mickey/Minie/3222/FTP/cfg1
    PATH2=/Mickey/Minie/3222/FTP/cfg2/bin
    PATH3=/Mickey/Minie/3222/FTP/cfg3/bin

You are almost done

print (gsub("perm_loc",b));

its prints the count of substitution

awk -F'=' -v a="MonsterHouse.Scully" \
          -v b="Mickey/Minie"  '{
                                  if($1 == "ORACLE_HOME" )
                                       printf "ORACLE_HOME=%s\n",a 
                             else if ( $2 ~ /perm_loc/ ){
                                       gsub("perm_loc",b);
                                       print
                                }
                                else
                                       print                     
                                }' file
1 Like

Try:

awk -F= -v a="MonsterHouse.Scully" -v b="Mickey/Minie" '{if ($1 ~ "ORACLE_HOME" ) printf "ORACLE_HOME=%s\n",a;
  else if ( $2 ~ /perm_loc/ ) {gsub("perm_loc",b); print} else print}' file
1 Like

Worked ! Thank-you Tigers.