Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

split file contents into files

25 views
Skip to first unread message

Harry

unread,
May 31, 2012, 10:05:56 PM5/31/12
to
I have extracted some scripts from an Oracle database using
the Toad query below. The script body is of datatype HUGHCLOB;
which means each script may be from 50 - 200+ lines.
N.B. Toad is a tool to query oracle database.


select
script_name
, script_type
, '>>>>>'
, script_body
from
script_table
order by
script_type, script_name;

The Toad query returned a single text file like this below.

What I want is an shell script, say, AWK, to parse
this all-in-one TOAD output file, to split each script
into its own script files, with names being script_name1.txt,
script_name2.txt, ... etc.

Any help appreciated.

--
script_name1,TypeA,>>>>>,
;comment line 1
;comment line 2
<blank line>
script 1 line 1
script 1 line 2
....
scrtp 1 last line
script_name2,TypeB,>>>>>,
/*
* comments for script 2
* ...
*/
script 2 line 1
script 2 line 2
...
--

TIA

Ed Morton

unread,
May 31, 2012, 10:10:36 PM5/31/12
to
based on the above, it seems like something like this would work:

awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}'

Ed.

Harry

unread,
May 31, 2012, 10:39:15 PM5/31/12
to
Ed Morton wrote...
Ed,

I got the folloing error.
I'm on cygwin with GNU awk 4.0.0.

$ awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}' <
TOAD_outfile.wri
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: expression for `>' redirection has
null string value

Any clue?

Harry

unread,
May 31, 2012, 10:47:04 PM5/31/12
to
Harry wrote...
>
>Ed Morton wrote...
[...]
>>based on the above, it seems like something like this would work:
>>
>>awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}'
>
>Ed,
>
>I got the folloing error.
>I'm on cygwin with GNU awk 4.0.0.
>
>$ awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}' <
>TOAD_outfile.wri
>awk: cmd. line:1: (FILENAME=- FNR=1) fatal: expression for `>' redirection
has
>null string value
>
>Any clue?

Oooppp... my bad, I forgot to trim my sql statement at the top
of my file, TOAD_outfile.wri.

After removing them ... your solution worked perfectly.

Thanks

Harry

unread,
Jun 1, 2012, 10:14:40 AM6/1/12
to
On May 31, 8:10 pm, Ed Morton <mortons...@gmail.com> wrote:
[...]
>
> based on the above, it seems like something like this would work:
>
> awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}'
>

The following line does not belong to the script body.
How could I skip this line showing up in the generated script file(s)?
--
script_name1,TypeA,>>>>>,
--

Thanks

Ed Morton

unread,
Jun 1, 2012, 10:58:57 AM6/1/12
to
awk -F, '/>>>>>/ {close(file); file=$1 ".txt"; next} {print> file}'

Ed

Harry

unread,
Jun 1, 2012, 10:28:03 AM6/1/12
to
On May 31, 8:10 pm, Ed Morton <mortons...@gmail.com> wrote:
[...]
>
> based on the above, it seems like something like this would work:
>
> awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}'

The following line does not belong to the script body.
How could I exclude it from the generated script files?

Harry

unread,
Jun 1, 2012, 4:24:43 PM6/1/12
to
On Jun 1, 8:28 am, Harry <harryooopot...@hotmail.com> wrote:
[...]
> > awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print > file}'
>
> The following line does not belong to the script body.
> How could I exclude it from the generated script files?
> --
> script_name1,TypeA,>>>>>,
> --

OK, I got it with this.

awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {if ($3 != ">>>>>")
print $0 >> file}'< TOAD_outfile.wri

Ed Morton

unread,
Jun 1, 2012, 4:34:47 PM6/1/12
to
you might want to check the response I posted this morning for a better solution.

Ed.

Harry

unread,
Jun 1, 2012, 11:20:56 PM6/1/12
to
Ed Morton wrote...
Yes, Agree.

I saw your reply after I sent mime.

Thanks a lot.


Ed Morton

unread,
Jun 2, 2012, 9:18:05 AM6/2/12
to
You're welcome. If you're going to be using awk, though, I recommend you get the
book "Effective Awk Programming, 3rd Edition" by Arnold Robins. There's a couple
of things in the script you posted above ("if (..)" and "print >>") that are red
flags to fundamental misunderstandings on awk semantics.

Ed.

Harry

unread,
Jun 2, 2012, 7:26:45 PM6/2/12
to
Ed Morton wrote...

[...]

>>> On 6/1/2012 3:24 PM, Harry wrote:
>>>> [...]
>>>>>> awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print> file}'
[...]
>You're welcome. If you're going to be using awk, though, I recommend you get
the
>book "Effective Awk Programming, 3rd Edition" by Arnold Robins. There's a
couple
>of things in the script you posted above ("if (..)" and "print >>") that are
red
>flags to fundamental misunderstandings on awk semantics.

Ed. I will certainly take your advice to get myself more into awk.

BTW, I tried out (print > file) and it worked and I guess it has to do
with awk being working on stream.

On the "if (..)" part, let me guess ... I should not use $3 due to
the fact that not every line has $3 ... isn't that what I did wrong?

Or you meant something else?

Thanks

Ed Morton

unread,
Jun 3, 2012, 11:46:14 AM6/3/12
to
On 6/2/2012 6:26 PM, Harry wrote:
> Ed Morton wrote...
>
> [...]
>
>>>> On 6/1/2012 3:24 PM, Harry wrote:
>>>>> [...]
>>>>>>> awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print> file}'
> [...]
>> You're welcome. If you're going to be using awk, though, I recommend you get
> the
>> book "Effective Awk Programming, 3rd Edition" by Arnold Robins. There's a
> couple
>> of things in the script you posted above ("if (..)" and "print>>") that are
> red
>> flags to fundamental misunderstandings on awk semantics.
>
> Ed. I will certainly take your advice to get myself more into awk.
>
> BTW, I tried out (print> file) and it worked and I guess it has to do
> with awk being working on stream.

The issue is that the semantics of > and >> are different in awk than in shell.
You should be using > in your script.
>
> On the "if (..)" part, let me guess ... I should not use $3 due to
> the fact that not every line has $3 ... isn't that what I did wrong?
>
> Or you meant something else?
>

Something else. Awk scripts are made up of:

<condition> { <action> }

statements. So you naturally should put your conditions in the condition part
rather than the action part. i.e. instead of:

{ if (<condition>) { <action> } }

you should simply write:

<condition> { <action> }

unless there's a good reason not to do that (which in this case there is not).

> Thanks
>

You're welcome.

Ed.
0 new messages