Link to home
Start Free TrialLog in
Avatar of dbguy0
dbguy0

asked on

Shell Script question. Replacing a word in all lines in file based on another field value

Hello all,
I have a file with many lines like below and several other small lines in it. For all lines that begin with CREATE DATABASE LINK  I have to replace <PWD> (i.e field 10) with <field 7>_ppp . field7 is not constant and it varies from one line to other. It needs to be dynamically sustituted. Shown it below in detail, could some one help with me with the substitution?

Before:CREATE DATABASE LINK "abc.com"  CONNECT TO testuser  IDENTIFIED BY <PWD> USING 'abc.com';

After replace, the line should become.

CREATE DATABASE LINK "abc.com"  CONNECT TO testuser  IDENTIFIED BY testuser_ppp USING 'abc.com';

Any help will be appreciated.
Avatar of ozo
ozo
Flag of United States of America image

perl i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, should be

awk '{ /^CREATE DATABASE LINK/ &&  $10 = $7."_app" ;print }'  /path-to-file
By the way, ozo, your script missiing "-" before i.bak
perl -i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file

I tested your and mine. Both works.
awk '{ /^CREATE DATABASE LINK/ &&  $10 = $7."_app" ;print }'  /path-to-file > /new-file
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial