• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • 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.
  • 4
2 Solutions
perl i.bak -lape '$F[9]="$F[6]_ppp"and $_="@F" if /^CREATE DATABASE LINK/' file
awk '/^CREATE DATABASE LINK/ { $10 = $7."_app"; print}'  /path-to-file
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
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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