Extracting lines based on identifiers into multiple files respectively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting lines based on identifiers into multiple files respectively
# 1  
Old 12-20-2011
Extracting lines based on identifiers into multiple files respectively

consider the following is the contents of the file

Code:
cat 11.sql
drop procedure if exists hoop1 ;
Delimiter $$
CREATE PROCEDURE hoop1(id int)
BEGIN
END
$$
Delimiter ;
.
.
.
.
drop procedure if exists hoop2;
Delimiter $$
CREATE PROCEDURE hoop2(id int)
BEGIN
END
$$
delimiter ;
.
.
DROP VIEW IF EXISTS view1;
 CREATE VIEW view1
 AS
 SELECT blah blah  FROM blah etc etc
 WHERE blah blah
 GROUP BY  id;
.
.
.
DROP VIEW IF EXISTS view2;
 CREATE VIEW view2
 AS
 SELECT blah blah  FROM blah etc etc
 WHERE blah blah
 GROUP BY  id;
.
.
.
drop procedure if exists hoop3;
Delimiter $$
CREATE PROCEDURE hoop2(id int)
BEGIN
END
$$
Delimiter ;

i tried

Code:
perl -lne '(/^\s*CREATE\s+PROCEDURE/i .. /^\s*\)\s+Delimiter/i) && print;' "11.sql" > onlyproc1.sql
 
perl -lne '(/^\s*CREATE\s+VIEW/i .. /^\s*\)\s+;/i) && print;' "11.sql" > onlyviews1.sql


its not working properly.. i want the all the procedures from CREATE PROCEDURES to Delimiter ( or it can be small case delimiter) in one file and also all views from CREATE VIEW to ";" which marks the end of that particular view in another file.... how to do this.. :-/ plz help Smilie
# 2  
Old 12-20-2011
Code:
$ nawk '/CREATE PROCEDURE/,/[dD]elimiter/' infile > create_procedure_outfile

Code:
$ nawk '/CREATE VIEW /,/;/' infile > create_view_outfile

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 12-20-2011
i am getting the error
-bash: nawk: command not found
what to do?
# 4  
Old 12-20-2011
Use awk or gawk (which may well be the same anyway, if you're running a Linux OS).
This User Gave Thanks to CarloM For This Post:
# 5  
Old 12-21-2011
Code:
echo "`sed -n "/$firstword/,/;/ w viewextract.sql" < onlyviews1.sql`"

where firstword is CREATE VIEW some view name.. similarly procedure name....
now my doubt is consider i have two procedures of same name.. so when i execute this
Code:
 echo "`sed -n "/$firstword/,/[dD]elimiter/ w extractproc.sql" < onlyproc1.sql`"

and if
Code:
cat onlyproc1.sql
CREATE PROCEDURE ABC(id int)
BEGIN
END
$$
Delimiter ;
CREATE PROCEDURE xyz(id int)
BEGIN
END
$$
Delimiter ;
CREATE PROCEDURE ABC(id int)
BEGIN
END
.
.
.etc

the sed command will write two procedures to the file extractproc.sql which are repeated.. is there any way to make sure the command takes only one procedure and writes it to the file...?
# 6  
Old 12-21-2011
Then expand your firstword pattern accordingly ..
# 7  
Old 12-21-2011
consider the onlyproc1.sql file which i mentioned above...
it has two procedures under the name ABC..
so if i use
Code:
firstword="CREATE PROCEDURE ABC"
echo "`sed -n "/$firstword/,/[dD]elimiter/ w extractproc.sql" < onlyproc1.sql`"

then extractproc.sql will have two procedures of same name... but i was the fle to have only one procedure...is there any way we could do it in one line... i could ofcourse do it using while loop by writing to another file till first procedure ends but its time consuming which leads to code inefficiency...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

3. Shell Programming and Scripting

Merging two tables including multiple ocurrence of column identifiers and unique lines

I would like to merge two tables based on column 1: File 1: 1 today 1 green 2 tomorrow 3 red File 2: 1 a lot 1 sometimes 2 at work 2 at home 2 sometimes 3 new 4 a lot 5 sometimes 6 at work (4 Replies)
Discussion started by: BSP
4 Replies

4. Shell Programming and Scripting

Extracting words and lines based on keywords

Hello! I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here: 1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script? For example: Given a keyword "world" in the line: ... (2 Replies)
Discussion started by: seemad
2 Replies

5. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

6. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

7. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

8. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

9. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

10. Shell Programming and Scripting

Extracting lines in file based on time

Hi, anyone has any ideas on how do we extract lines from a file with format similiar to this: (based on current time) Jun 18 00:16:50 .......... ............. ............ Jun 18 00:17:59 .......... ............. ............ Jun 18 01:17:20 .......... ............. ............ Jun 18... (5 Replies)
Discussion started by: faelric
5 Replies
Login or Register to Ask a Question