extracting block of lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting block of lines from a file
# 1  
Old 12-22-2011
extracting block of lines from a file

consider the input file which i am dealing with looks like this..

Code:
#cat 11.sql
 
create table abc (
.
.
.
) engine=Innodb ;
.
.
etc
.
.
.
create table UsM (
 blah
blah
blah
) engine=Innodb ;
.
.
.
drop procedure if exists b77 ;
Delimiter $$
CREATE PROCEDURE b77(id int)
BEGIN
END
$$
Delimiter ;
.
.
.
drop procedure if exists a77;
Delimiter $$
CREATE PROCEDURE a77(id int)
BEGIN
END
$$
Delimiter ;
.
.
CREATE PROCEDURE gNodes (LEVEL INT)
BEGIN
 blah
blah
.
.
        SET rootName = (SELECT DirectoryCategory.name FROM DirectoryCategory WHERE DirectoryCategory.id IN (SELECT DirectoryTree.directoryCategoryId FROM DirectoryTree WHERE DirectoryTree.id=rootId)) ;
    
        DROP TABLE IF EXISTS ResultNodes ;
        CREATE TABLE ResultNodes (id INT, NAME TEXT) ;
  END IF ;
  
  IF LEVEL>0 THEN
        SET parentPrefix = CONCAT(pa etc etc
.
.
END
$$
Delimiter ;

here somewhere in the middle of a procedure there is a "create table" thing... i am perfroming some action so that all create table to Engine=innodb is exctarcted to a file.. that command used is as below

Code:
perl -lne '(/^\s*create\s+table/i .. /^\s*\)\s+engine=Innodb/i) && print;' "11.sql" > onlytables.sql

but the problem is since there is a create table line in between a procedure even it is considered as a table and extracted... how to avoid this... i could perfrom this action in a while loop where it extractes all tables to a file and break when it encounters any line with create procedure in it( since create table lines come in first part of file and procedures comes next) but i need one liners to perfrom this action... any help is deeply appreciated .... SmilieSmilie
# 2  
Old 12-22-2011
reverse logic:
Code:
perl -e 'print reverse <>' "11.sql"|perl -lne '(/\s+engine=Innodb/i .. /create\s+table/i) && print;'|perl -e 'print reverse <>'

This User Gave Thanks to Klashxx For This Post:
# 3  
Old 12-22-2011
Quote:
Originally Posted by vivek d r
Code:
perl -lne '(/^\s*create\s+table/i .. /^\s*\)\s+engine=Innodb/i) && print;' "11.sql" > onlytables.sql

Can you remove the case-insensitive character and try, if that does not matter really..?
Code:
# awk variant..
awk '/create table|engine/{++i}i{x=x"\n"$0}i==2{print x;x=i=""}' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 12-22-2011
thanks guys.... both the commands (of @Klashxx and of @michaelrozar17) worked... you guys saved my life.. :-)... thanks a lot :-)
# 5  
Old 12-22-2011
In one perl call:
Code:
perl -e '@a=reverse <>;foreach (@a){push (@b,$_) if /\s+engine=Innodb/i .. /create\s+table/i};print reverse(@b),"\n"' "11.sql"

This User Gave Thanks to Klashxx For This Post:
# 6  
Old 12-22-2011
thanks even this is working :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting a block of text from a large file using variables?

Hi UNIX Members, I've been tasked with performing the following: Extract a block of data in column form #This data changes each time, therefore automating future procedures Please Note the following: line = reading a line from a file_list that leads to the data The filename is called... (16 Replies)
Discussion started by: Klor
16 Replies

2. Shell Programming and Scripting

extracting lines from a file with similar first name

consider i have two files cat onlyviews1.sql CREATE VIEW V11 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V22 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V33 AS (10 Replies)
Discussion started by: vivek d r
10 Replies

3. 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

4. UNIX for Advanced & Expert Users

Extracting Lines in a file

Hi, We have a file where we need to extract the lines of a file based on the first three characters of every line in that file. Eg: RAM hill station SAM student RAM rose SAM apple RAM india SAM australia RAM happy RAM heaven SAM smile RAM glow So, all the lines that start... (5 Replies)
Discussion started by: pyaranoid
5 Replies

5. Shell Programming and Scripting

extracting specific lines from a file

hi all, i searched in unix.com and accquired the following commands for extracting specific lines from a file .. sed -n '16482,16482p' in.sql > out.sql awk 'NR>=10&&NR<=20' in.sql > out.sql.... these commands are working fine if i give the line numbers as such .. but if i pass a... (2 Replies)
Discussion started by: sais
2 Replies

6. UNIX for Dummies Questions & Answers

extracting lines from a file

i want to extract lines 5 and 7 from a txt file which contains nearly 20 entries how to do it also i want to check whether the 42nd character is 'S' in that line suggestions welcome (4 Replies)
Discussion started by: trichyselva
4 Replies

7. Shell Programming and Scripting

Extracting the lines between 2 strings of a file

Hi, I have a sql file and i need to extract the table names used in the sql file using a unix script. If i can extract the lines between the keywords 'FROM' and 'WHERE' in the file, my job is done. can somebody tell me how to do this using a shell script. If u can just let me know, how to... (2 Replies)
Discussion started by: babloo
2 Replies

8. Shell Programming and Scripting

extracting lines from a file

Hi all, I need to extract some lines from a file based on a condition. For ex: My file will contain 50 lines and i need to extract line which has "File" in it and then the line which has "date" in it. Which command will be the most efficient way to do it. I have tried two ways 1.... (1 Reply)
Discussion started by: pradeepthanraj
1 Replies

9. Shell Programming and Scripting

Help required regarding Extracting lines from a file

I have a file containing the following contents All of us, including Zippy, our dog All of us, including Zippy and Zippy All of us, including Zippy and Zippy and Zelda Testing All of us Zippy Now, i wanna grep and get the lines which has only one occurance of word Zippy and starting with... (1 Reply)
Discussion started by: google_ever
1 Replies

10. Shell Programming and Scripting

Parsing file and extracting the useful data block

Greetings All!! I have a very peculiar problem where I have to parse a big text file and extract useful data out of it with starting and ending block pattern matching. e.g. I have a input file like this: sample data block1 sample data start useful data end sample data block2 sample... (5 Replies)
Discussion started by: arminder
5 Replies
Login or Register to Ask a Question