We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

dbguy0
dbguy0 asked
on
Medium Priority
304 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
perl i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file
Top Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2011

Commented:
Sorry, should be

awk '{ /^CREATE DATABASE LINK/ &&  $10 = $7."_app" ;print }'  /path-to-file
Top Expert 2011

Commented:
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
Top Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.