Extract a block of text | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Extract a block of text

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2013
kiki_riki_miki kiki_riki_miki is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 8 January 2014, 10:38 AM EST
Location: Massy
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Extract a block of text

Hello all,

I am working on a script which should parse a large file called input.txt which contains table definitions, index definitions and comments like these ones:




Code:
------------------------------------------------
-- DDL Statements for table "CMWSYS"."CMWD_TEC_SUIVI_TRT"
------------------------------------------------
 

CREATE TABLE "CMWSYS"."CMWD_TEC_SUIVI_TRT"  (
          "ORDER_ID" VARCHAR(20) , 
          "PERIODE_DATE" VARCHAR(50) , 
          "NOM_JOB" VARCHAR(100) , 
          "D_START" TIMESTAMP , 
          "D_END" TIMESTAMP , 
          "T_ELAPSED" TIME , 
          "STATUS" VARCHAR(50) )   
         IN "CMW_DA16DN01" INDEX IN "CMW_IX16DN01" ;





Code:
------------------------------------------------
-- DDL Statements for table "CMWSYS"."CMWD_REF_MARCHE_AFFECTATION"
------------------------------------------------
 

CREATE TABLE "CMWSYS"."CMWD_REF_MARCHE_AFFECTATION"  (
          "CD_CSM" SMALLINT NOT NULL , 
          "CD_SSE_3EME_CAR" CHAR(1) NOT NULL , 
          "CD_TYP_PERS" CHAR(3) NOT NULL , 
          "DT_VAL_DEB" DATE , 
          "CLE_SS_COMPRT" SMALLINT NOT NULL , 
          "DT_VAL_FIN" DATE , 
          "TOP_COURANT" CHAR(1) , 
          "TOP_ANNUL" CHAR(1) , 
          "DT_MAJ_ETL" TIMESTAMP )   
         IN "DIM_DA16DN01" INDEX IN "DIM_IX16DN01" ; 

COMMENT ON COLUMN "CMWSYS"."CMWD_REF_ZONE_MONETAIRE"."DT_MAJ_ETL" IS 'Date ETL chargement du flux';

COMMENT ON COLUMN "CMWSYS"."CMWD_REF_ZONE_MONETAIRE"."TOP_ANNUL" IS '1 annulé, 0 sinon';




The script should extract each table definition in one separate file (normal txt file) called dbname_tablename.txt. The "dbname" is the first word in the quotation marks after CREATE TABLE statement and the "tablename" is the string characters included in quotation marks after the '.' sign (for this case CMWSYS_CMWD_TEC_SUIVI_TRT or CMWSYS_CMWD_REF_MARCHE_AFFECTATION). All the others definitions (indexes) and the comments should be ignored..

Do you have any idea how I can manage this task? I still have no result with my humble script I am working at.

Thanks

Last edited by Scrutinizer; 03-07-2013 at 07:32 AM.. Reason: code tags (AGAIN)
Sponsored Links
    #2  
Old 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 October 2014, 9:32 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
If you don't provide more clue about the expected format's result, i am afraid people won't be able to help you much.
Sponsored Links
    #3  
Old 03-07-2013
kiki_riki_miki kiki_riki_miki is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 8 January 2014, 10:38 AM EST
Location: Massy
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Extract a block of text

I edited the description and I guess it should be better now. Thank you, ctsgnb for this observation.
    #4  
Old 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 October 2014, 9:32 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
Depending on your Database, some specific tool should exist to perform such kind of task. I think you should consider this approach rather than "reinventing the wheel"
Sponsored Links
    #5  
Old 03-07-2013
kiki_riki_miki kiki_riki_miki is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 8 January 2014, 10:38 AM EST
Location: Massy
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Extract a block of text

I do not intend to reinvent the wheel, but to find a useful tool to make this conversion. I found some tools, but all of them are not free of use, unless you pay for the license.
Sponsored Links
    #6  
Old 03-07-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
Could you show a larger sample? Telling where the definitions begin and end can be awkward unless they all begin the same way, with a comment...

You can use awk for this though, I'm pretty sure. It has a "block" mode, where it can read entire sections delimited by blank lines.
Sponsored Links
    #7  
Old 03-07-2013
kiki_riki_miki kiki_riki_miki is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 8 January 2014, 10:38 AM EST
Location: Massy
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Extract a block of text

Attached you will find the input.txt file containing all the table definitions.
Attached Files
File Type: txt input.txt.txt (2.57 MB, 33 views)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grepping text block by block by using for loop anushree.a Shell Programming and Scripting 12 09-24-2012 06:46 AM
How to extract block from a file? ran123 Shell Programming and Scripting 5 05-04-2011 08:54 AM
[Awk] Extract block of with a particular pattern sandeepk1611 Shell Programming and Scripting 7 02-15-2011 03:50 PM
Extract a block of text?? marcozd Shell Programming and Scripting 10 01-25-2011 03:40 PM
extract block in file sskb UNIX for Dummies Questions & Answers 5 10-25-2001 10:29 AM



All times are GMT -4. The time now is 05:22 PM.