[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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.
0
dbguy0
Asked:
dbguy0
  • 4
2 Solutions
 
ozoCommented:
perl i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file
0
 
wesly_chenCommented:
awk '/^CREATE DATABASE LINK/ { $10 = $7."_app"; print}'  /path-to-file
0
 
wesly_chenCommented:
Sorry, should be

awk '{ /^CREATE DATABASE LINK/ &&  $10 = $7."_app" ;print }'  /path-to-file
0
 
wesly_chenCommented:
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
0
 
wesly_chenCommented:
Hi, I will be on vacation for 10 days start 6/17/2011. Please click "Request Attention" if you need help during my absent. Other experts will assist you.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now