Operating System - HP-UX
1834130 Members
3225 Online
110064 Solutions
New Discussion

Re: Text Substitutions based on files

 
Don Spare
Regular Advisor

Text Substitutions based on files

I need to be able to search a file for a word then replace some words several lines later. The first word will be a key to finding the values in another file that will be substituted in the later line. I was thinking of using awk but I don't know enough to be able to do this. The specific example is an Oracle based CREATE TABLE statement. First find the line with CREATE TABLE on it and use the 3rd word (table name) to find a value in an external file which has the table size in it. Then scan down to the 'storage' line and make the substitutions. Repeat for every occurrence of CREATE TABLE.

Is awk the right tool? Is there something better?
2 REPLIES 2
Vincenzo Restuccia
Honored Contributor

Re: Text Substitutions based on files

You can see sed -e "s/word/Word/g",verify man sed.
Rodney Hills
Honored Contributor

Re: Text Substitutions based on files

If you are well versed in AWK, you could probabily do it, but I would use PERL instead.
Here is a sample script...

# First read in file with new sizes (each record is TABLE_NAME,NEW_SIZE)
open(INP,"while() { chop; ($tbl,$siz)=split(",",$_); $size{$tbl}=$siz; }
close(INP);
open(INP,"open(OUT,">newscript");
while()
/^CREATE TABLE (\S+)/ && do { $cretbl=$1; }
/^storage (\S+)/ && do {
if($newsiz=$size{$cretbl}) { s/$1/$newsiz/; }
}
print OUT $_;
}
There be dragons...