extract DDL - output every match to separate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract DDL - output every match to separate file
# 8  
Old 09-12-2011
Quote:
Originally Posted by CactusMoon
example:
Code:
-- TABLE001
CREATE UNIQUE INDEX ATABL001
    ON TABLE001
     (COLUMN1 ASC,
      COLUMN2 ASC,
      COLUMN3 ASC,
      COLUMN4 ASC);
COMMIT;
 
CREATE INDEX BTABL001
    ON TABLE001
     (COLUMN3 ASC,
      COLUMN4 ASC);
COMMIT;
 
CREATE INDEX CTABL001
    ON TABLE001
     (COLUMN3 ASC);
COMMIT;
 
-- TABLE002
CREATE UNIQUE INDEX ATABL002
    ON TABLE002
     (COLUMN1 ASC,
      COLUMN2 ASC,);
COMMIT;
...

every single create index statement should go in its separate file e.g. index ATABL001 to ATABL001.ddl, BTABL001 to BTABL001.ddl, ... i mean the whole statement to the semicolon or the commit...
Code:
$
$ # list all files in current directory
$ ls
ddl_stmts.sql
$
$ # display the contents of "ddl_stmts.sql"
$ cat -n ddl_stmts.sql
     1  -- TABLE001
     2  CREATE UNIQUE INDEX ATABL001
     3      ON TABLE001
     4       (COLUMN1 ASC,
     5        COLUMN2 ASC,
     6        COLUMN3 ASC,
     7        COLUMN4 ASC);
     8  COMMIT;
     9
    10  CREATE INDEX BTABL001
    11      ON TABLE001
    12       (COLUMN3 ASC,
    13        COLUMN4 ASC);
    14  COMMIT;
    15
    16  CREATE INDEX CTAB001
    17      ON TABLE001
    18       (COLUMN3 ASC);
    19  COMMIT;
    20
    21  -- TABLE002
    22  CREATE UNIQUE INDEX ATABL002
    23      ON TABLE002
    24       (COLUMN1 ASC,
    25        COLUMN2 ASC,);
    26  COMMIT;
    27
$
$ # run the Perl one-liner
$
$ ##
$ perl -lne 'if (/^create.*?index (.*?)$/i) {open (FH, ">", "$1.sql"); print FH $_; $in=1}
             elsif (/;\s*$/ && $in) {print FH $_; close (FH); $in=0}
             elsif ($in) {print FH $_}
            ' ddl_stmts.sql
$
$
$ # list all files in current directory again
$ ls -1
ATABL001.sql
ATABL002.sql
BTABL001.sql
CTAB001.sql
ddl_stmts.sql
$
$ # display the contents of the newly created SQL files
$
$ cat -n ATABL001.sql
     1  CREATE UNIQUE INDEX ATABL001
     2      ON TABLE001
     3       (COLUMN1 ASC,
     4        COLUMN2 ASC,
     5        COLUMN3 ASC,
     6        COLUMN4 ASC);
$
$ cat -n ATABL002.sql
     1  CREATE UNIQUE INDEX ATABL002
     2      ON TABLE002
     3       (COLUMN1 ASC,
     4        COLUMN2 ASC,);
$
$ cat -n BTABL001.sql
     1  CREATE INDEX BTABL001
     2      ON TABLE001
     3       (COLUMN3 ASC,
     4        COLUMN4 ASC);
$
$ cat -n CTAB001.sql
     1  CREATE INDEX CTAB001
     2      ON TABLE001
     3       (COLUMN3 ASC);
$
$

tyler_durden
# 9  
Old 09-12-2011
Did you try nawk like I suggested?

The difference is that gawk/nawk have close. Open too many files at once in awk and you can hit its limit -- and that can be pretty small on some systems!
# 10  
Old 09-12-2011
I guess, awk also has close(). I never thought about that.

--ahamed
# 11  
Old 09-12-2011
Quote:
Originally Posted by ahamed101
I guess, awk also has close().
Not all awk, especially not on "true" UNIXes where shell commands usually don't have any non-POSIX options or features. They'd rather add a 'nawk' than risk breaking vanilla awk's backwards compatibility in someone's 30-year-old shell script. In Solaris, nawk is available under the silly path of /usr/xpg4/bin/awk...

Linux/GNU usually has nothing but gawk. awk is just a symlink. Things like this can occasionally make testing UNIX scripts on Linux difficult -- features that seem so obvious they should be everywhere turn out to be missing...

Last edited by Corona688; 09-12-2011 at 02:59 PM..
# 12  
Old 09-13-2011
Quote:
Originally Posted by ahamed101
Code:
awk '/^--/{next}/CREATE UNIQUE INDEX|CREATE INDEX/{file=$NF}{print>file".ddl"}' input.ddl

Assuming this file has only index create statements!

--ahamed
there are other statements in the file too e.g. CREATE TABLE. how would the above script look like?

Last edited by CactusMoon; 09-13-2011 at 04:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match file and extract the output

Hi All I have 2 file . I need match the files based on key and then form a third file which have the matching values FILE1: 10264;K*AD 10265;K*AIR 10266;K*AUTO 10267;K*BABY 10268;K* FOOD FILE2: 10264;1055.83 10265;716.94 10267;331.80 10268;23283.33 OUTPUT (Needed)... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. Programming

creating separate output file for each input file in python

Experts, Need your help for this. Please support My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput) Input files File 1(1.1.1.1.csv) a,b,c 43,17104773,3 45,17104234,4 File 2(2.2.2.2.csv) a,b,c 43,17104773,1... (2 Replies)
Discussion started by: as7951
2 Replies

3. Shell Programming and Scripting

Trying to use diff output to compare to a separate file

I have two files: smw:/working/iso_testing # cat a QConvergeConsoleCLI-1.1.03-49.x86_64.rpm aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm acpica-20140724-2.1.2.i586.rpm test.rpm smw:/working/iso_testing # cat b QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Discussion started by: jedlund21
12 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

6. Shell Programming and Scripting

Extract table name from DDL

How can I extract table name from the different DDL statement like ALTER TABLE CREATE TABLE etc Basically I have to parse thr the any of the DDL statement and verify if that DDL statement is implemented by DBA or not. how can i do this efficiently in Kornshell scripting. (2 Replies)
Discussion started by: gayathree
2 Replies

7. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

8. Shell Programming and Scripting

Help required to parse Oracle imp show=y output to DDL Commands

Hi, I generated an Oracle schema DDL script file using the show=y option of the Oracle import utility but the file that it generates needs a little more formating before we can run this as simple DDL comands to generate the schema at Target using the script file.Here is the simplified output of... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

extract x lines after a pattern - place each result in separate file

Hi all, I have many files that have 1 or more occurrences of the information I want. There are two distinct sets of information. I want get this info and place each occurrence in its own file. The 3 lines before one set are this grid 00 01 02 16 17 18 **40 lines of code I want to... (5 Replies)
Discussion started by: gobi
5 Replies

10. UNIX for Dummies Questions & Answers

Output of command to 2 separate file?

How would I echo the same results to two files? One is a running log, and the other is a cache, or sort. echo "Hello World" >> /Logs/File1 & /tmp/file2 I would just copy it from one place to the other, but the Log keeps history, where I want the /tmp to hold only stuff per session. I tried... (2 Replies)
Discussion started by: TheCrunge
2 Replies
Login or Register to Ask a Question