Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I've also posted this in the 1

Status
Not open for further replies.

butterfm

Programmer
Feb 15, 2002
132
GB
Hi,

I've also posted this in the UNIX scripting forum but thought it may be more appropriate here.

I'm struggling a bit with the following problem. I've tried awk and sed but can't get a solution.

I have a file containing text similar to the following (although I have cut bits out to fit it on this page)

NOT NULL, "PAGEHEIGHT" NUMBER(10, 0) NOT NULL) PCTFREE
40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 20480 NEXT
1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1
REM FREELIST GROUPS 1) TABLESPACE "FIN_CODAT" ;
REM ... 2 rows
CONNECT CUST_TEST;
CREATE UNIQUE INDEX "CUST_TEST"."OAS_ASM_INDEX1" ON "OAS_ASM" ("CODE" )
PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE (INITIAL 20480 NEXT 20480
MINEXTENTS 1 MAXEXTENTS 2147483645 ;

What I need to do it replace the text, accross multiple lines, between the word "PCTFREE" and the next occuring ";" with another string, although I would want to keep semi colon ";"

e.g. Replacing the text with the word DOG would give me

NOT NULL, "PAGEHEIGHT" NUMBER(10, 0) NOT NULL) DOG ;
REM ... 2 rows
CONNECT CUST_TEST;
CREATE UNIQUE INDEX "CUST_TEST"."OAS_ASM_INDEX1" ON "OAS_ASM" ("CODE" )
DOG ;

I have managed this with sed when the text I want to replace only spans 2 lines but not when it spans more than 2 lines.

Any help with this would be greatly appreciated.

Thanks, Matt.
 
something like should get you started:

nawk -f sql.awk sql.txt

#-------------------- sql.awk
BEGIN {
#RS=""
PATstart="PCTFREE"
PATend=";"

PATsubst="DOG";

}

{
if(!found && match($0, PATstart)) {
found=1;
printf("%s", substr($0, 1, RSTART-1));
next;
}

if( found && match($0, PATend)) {
printf("%s", PATsubst)
printf("%s\n", substr($0, RSTART));
found=0;
}

if (!found) print
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
ooops - didn't see the that one.

#------------------ sql.awk

BEGIN {
#RS=&quot;&quot;
PATstart=&quot;PCTFREE&quot;
PATend=&quot;;&quot;

PATsubst=&quot;DOG&quot;;

}

{
if(!found && match($0, PATstart)) {
found=1;
printf(&quot;%s&quot;, substr($0, 1, RSTART-1));
next;
}

if( found && match($0, PATend)) {
printf(&quot;%s%s\n&quot;, PATsubst, PATend);
if (RSTART+RLENGTH+1 < length($0))
printf(&quot;%s\n&quot;, substr($0, RSTART+RLENGTH+1));
found=0;
next;
}

if (!found) print
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks vgersh99,

It works a treat and I've learnt something in the process.

Much appreciated,
Matt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top