Extract specific lines from a file

Hi,

I have a file which contains DDL statements- CREATE TABLE, CREATE INDEX, ALTER TABLE etc.
I have to only pick CREATE TABLE statements from the file-

Source :
----------------------------------------------
--DDL for table abc
--------------------------------------------

CREATE TABLE abc(
col1 integer,
col2 varchar2(20),
col3 varchar2(50)
);

CREATE INDEX <statements>
<statements>

Output file should have only the lines for CREATE TABLE:

CREATE TABLE abc(
col1 integer,
col2 varchar2(20),
col3 varchar2(50)
);

So I have to process through the file and only pick the CREATE TABLE statements (multiple instances) and output to an output file.
Could you please help me writing a shell script for the same?

Try this,

awk '{if(/CREATE TABLE/){f=1;print;next} if (f==1 && $0 ~ /\);/){f=0;print;} if (f==1) { print}}' inputfile
1 Like

Thanks Pravin.. will try and post the result ..

Alternatively:

awk '/CREATE TABLE/,/^);/' infile
awk '/CREATE TABLE/' RS= infile
2 Likes

We can do same thing with sed as well

sed -n '/CREATE TABLE/,/^);/p' inputfile
1 Like