formating sql file using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formating sql file using awk or sed
# 1  
Old 09-30-2010
formating sql file using awk or sed

Hi,
I have a file where I would like to add a prompt type object_name
statement before every create commnad

Code:
create or replace force view test_view_01
(
col1
col2
col3
)
as
   (select a,b,c from sometable );
create or replace view test_view_02
(
col4
col5
col6
)
as
   (select x,y,z from sometable );
create or replace view test_view_03
(
col8
col9
col1
)
as
   (select q,w,e from sometable );

create or replace package test_pkg
as
function f1
( blah in b1%type
  blah1 in b2%type)
return tab1 pipelined;
);
  
create or replace package body test_pkg
as

Code:
prompt view test_view_01
create or replace force view test_view_01
(
col1
col2
col3
)
as
   (select a,b,c from sometable );
prompt view test_view_02
create or replace view test_view_02
(
col4
col5
col6
)
as
   (select x,y,z from sometable );
   
   
prompt view test_view_03
create or replace view test_view_03
(
col8
col9
col1
)
as
   (select q,w,e from sometable );
prompt package test_pkg
create or replace package test_pkg
as
function f1
( blah in b1%type
  blah1 in b2%type)
return tab1 pipelined;
);
  
prompt package body test_pkg
create or replace package body test_pkg
as

# 2  
Old 09-30-2010
Quote:
Originally Posted by jville
...
I have a file where I would like to add a prompt type object_name
statement before every create commnad
...
Code:
$
$
$ cat -n f27.sql
     1  create or replace force view test_view_01
     2  (
     3  col1
     4  col2
     5  col3
     6  )
     7  as
     8     (select a,b,c from sometable );
     9  create or replace view test_view_02
    10  (
    11  col4
    12  col5
    13  col6
    14  )
    15  as
    16     (select x,y,z from sometable );
    17  create or replace view test_view_03
    18  (
    19  col8
    20  col9
    21  col1
    22  )
    23  as
    24     (select q,w,e from sometable );
    25
    26  create or replace package test_pkg
    27  as
    28  function f1
    29  ( blah in b1%type
    30    blah1 in b2%type)
    31  return tab1 pipelined;
    32  end;
    33  /
    34
    35  create or replace package body test_pkg
    36  as
    37    function f1
    38    ( blah in b1%type
    39      blah1 in b2%type)
    40    return tab1 pipelined
    41    is
    42    begin
    43      null;
    44    end;
    45  end;
    46  /
    47
$
$ perl -lne 'if (/^create.*\b(table|view|package)\b(.*)$/){print "prompt $1$2\n$_"} else {print}' f27.sql
prompt view test_view_01
create or replace force view test_view_01
(
col1
col2
col3
)
as
   (select a,b,c from sometable );
prompt view test_view_02
create or replace view test_view_02
(
col4
col5
col6
)
as
   (select x,y,z from sometable );
prompt view test_view_03
create or replace view test_view_03
(
col8
col9
col1
)
as
   (select q,w,e from sometable );
prompt package test_pkg
create or replace package test_pkg
as
function f1
( blah in b1%type
  blah1 in b2%type)
return tab1 pipelined;
end;
/
prompt package body test_pkg
create or replace package body test_pkg
as
  function f1
  ( blah in b1%type
    blah1 in b2%type)
  return tab1 pipelined
  is
  begin
    null;
  end;
end;
/
 
$
$

tyler_durden
# 3  
Old 09-30-2010
Perfect ! works like charm. you are the man ! Thank-you so much.

---------- Post updated at 12:48 PM ---------- Previous update was at 12:38 PM ----------

one more question. how do I ignore case. For e.g in the above text for create or objects names?
# 4  
Old 09-30-2010
Quote:
Originally Posted by jville
...how do I ignore case. For e.g in the above text for create or objects names?
Use the "i" modifier -

Code:
perl -lne 'if (/^create.*\b(table|view|package)\b(.*)$/i){print "prompt $1$2\n$_"} else {print}' f27.sql

tyler_durden
# 5  
Old 10-01-2010
Code:
awk '/^create/{print "prompt", $NF}1' infile

# 6  
Old 10-01-2010
Code:
$ ruby -ane '$_="prompt #{$F[3..-1].join(" ")}\n#{$_}" if /^create/;puts $_' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with ... Formating the file using awk

I have a file like below position1 0 7802 7802 0 client1 - - position1 8 8032 8032 0 client1 ... (7 Replies)
Discussion started by: manas_ranjan
7 Replies

2. UNIX for Beginners Questions & Answers

awk HTML Conditional Formating

I am receiving the below output in text format. The output is converted to HTML table using the code mentioned below output in text LogDate DayOfWeek/Hours _0_ _1_ _2_ _3_ _4_ _5_ _6_ _7_ _8_ _9_ _10_ _11_ _12_ _13_ _14_ _15_ _16_ ... (3 Replies)
Discussion started by: Dumpi16
3 Replies

3. Shell Programming and Scripting

File formating

I need to create a fixed width file based on the column lengths. lets assume I have six(this may be dynamic) fields each are of different length column1=6 #size of the column column2=3 column3=2 column4=3 column5=4 column6=5 I tried below code snippet but it is not working echo... (4 Replies)
Discussion started by: gvkumar25
4 Replies

4. Shell Programming and Scripting

Using awk and sed to modify a create sql script

Hi, I have a file which contains the following data claim_src|clm_id,typ_id pat_src|pat_id prov_src|prov_id,clm_id,prov_name The first field is table name and second field is primary keys of the table Now I have three files which contain ddl of each table. clam_src.sql... (4 Replies)
Discussion started by: wahi80
4 Replies

5. Shell Programming and Scripting

sql dump file - how to get certain values using sed

Hi, I have a dumpfile.sql - ========Start of file================= CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */; CREATE TABLE `test_table1` ( `id` int ) AUTO_INCREMENT=12 CREATE TABLE `test_table2` ( `id` int ) AUTO_INCREMENT=120... (5 Replies)
Discussion started by: ashokvpp
5 Replies

6. Shell Programming and Scripting

sed parse a lign into a new sql file

Hi everyone, another question while using sed. my sed statement should parse every line in a file and store all "i" variable item a a new file. any wrong arguments here? Thanks a million. task_name => name, object_type => 'TABLE', attr1 => 'TestR3', attr2 => '$i', for i... (4 Replies)
Discussion started by: sundaygeek
4 Replies

7. Shell Programming and Scripting

File formating (sed command)

I am new ot unix. Is there one or two lines of command (sed may be) to format a file. My source is.. <tag1> <tag2> <datatag1> data1 </datatag1> <datatag2> data2 </datatag2> <datatag3> data3 </datatag3> <datatag4> data1 </datatag3> </tag2> </tag1> (2 Replies)
Discussion started by: reachsam11
2 Replies

8. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

9. Shell Programming and Scripting

AWK: formating number without printf

Hello, I wrote a script that does lot of things, and I would like to change the format of a number but without printing it now (so I don't want to use printf as it will print the value immediately). Schematically here is what I have: awk 'BEGIN{number=0.01234567} $1==$2{$3=number}... (5 Replies)
Discussion started by: jolecanard
5 Replies

10. Shell Programming and Scripting

SED + Regex + SQL Input file

Here's the problem... I have a mysqldump file and I need to put single quotes around the date/time timestamp. So for example I have a line like: INSERT INTO attachments VALUES (1,182,2004-08-06 09:24:04,'description'... and I need it to become INSERT INTO attachments VALUES... (10 Replies)
Discussion started by: primal
10 Replies
Login or Register to Ask a Question