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
# 1  
Old 09-12-2011
extract DDL - output every match to separate file

Hi,
i want to extract the 'CREATE INDEX' or 'CREATE UNIQUE INDEX' statements from a ddl file and output each match to a separate file. i was looking around the net but couldnīt find anything.

a possible sed-script could be:
Code:
sed -n '/CREATE[A-Za-z ]*INDEX*/,/COMMIT/p' filename.ddl

but i couldnīt find out how to output each match to a separate file. every CREATE-statement should go to its own file, while the filename is the name of the index.

any idea? is sed the right choice for this?

thank you
# 2  
Old 09-12-2011
can you provide some sample statements?

--ahamed
# 3  
Old 09-12-2011
Looking for a program to extract SQL statements is like looking for a programming language to print the letter 'A'. You're probably not going to find someone who had the exact same problem as you and solved it the exact same way...

nawk or gawk would be better.
Code:
$ echo "CREATE INDEX" | gawk '/CREATE([\t ]+UNIQUE)?[\t ]+INDEX/ { print $0 > NR ".txt" ; close(NR ".txt"); }'
$ cat 1.txt
CREATE INDEX
$

# 4  
Old 09-12-2011
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...
# 5  
Old 09-12-2011
Code:
awk '/^--/{next}/CREATE UNIQUE INDEX|CREATE INDEX/{file=$NF}{print>file".ddl"}' input.ddl

Assuming this file has only index create statements!

--ahamed

Last edited by ahamed101; 09-12-2011 at 02:33 PM.. Reason: Assumption added, optimized, bad me :(, optimized again!
# 6  
Old 09-12-2011
Then:
Code:
gawk 'BEGIN { FILE=1 }
/CREATE([\t ]+UNIQUE)?[\t ]+INDEX/ { if(W) close(FILE ".txt");  FILE++; W=0 }
{ print $0 > FILE ".txt" ; W=1 }' < infile

# 7  
Old 09-12-2011
Quote:
Originally Posted by ahamed101
Code:
awk '/^--/{next}/CREATE UNIQUE INDEX|CREATE INDEX/{file=$NF;print>file".ddl";next}{print>file".ddl"}' input.ddl

Assuming this file has only index create statements!

--ahamed

this one worked for me! great! thank you!

couldnīt try the other one because i currntly donīt have gawk installed.
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