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.
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.
perl i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, should be
awk '{ /^CREATE DATABASE LINK/ && $10 = $7."_app" ;print }' /path-to-file
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.