awk script to split text file content to multiple files

  • Follow


I have an input text file like this.
------------------------
DEFINE QLOCAL ('Q1') +
       ... number of lines
       ...
       REPLACE

DEFINE QLOCAL ('Q2') +
       ... number of lines
       ...
       REPLACE

DEFINE QALIAS ('A1') +
       ... number of lines
       ...
       REPLACE

DEFINE QALIAS ('A2') +
       ... number of lines
       ...
       REPLACE

------------------------

The input file contains other definitions as well, such as
QREMOTE, LISTENER, etc. But I use the above for the sake
of simplicity.

I want to split the conent of this input file so that
each segment (record) of text (blank line as record separator)
will be copied to text files, like this :

../QLOCAL/Q1 <-- the define statement as file content
../QLOCAL/Q2
../QALIAS/A1
../QALIAS/A2

That is, the subfolder is the name (QLOCAL or QALIAS) after
the word DEFINE. While the filename corresponds to the string
surrounded by ('<as_filename>').

Appreciate your help.
0
Reply harryooopotter2 (20) 8/12/2010 10:19:08 PM

On 13/08/10 00:19, Harry wrote:
> I have an input text file like this.
> ------------------------
> DEFINE QLOCAL ('Q1') +
>        ... number of lines
>        ...
>        REPLACE
> 
> DEFINE QLOCAL ('Q2') +
>        ... number of lines
>        ...
>        REPLACE
> 
> DEFINE QALIAS ('A1') +
>        ... number of lines
>        ...
>        REPLACE
> 
> DEFINE QALIAS ('A2') +
>        ... number of lines
>        ...
>        REPLACE
> 
> ------------------------
> 
> The input file contains other definitions as well, such as
> QREMOTE, LISTENER, etc. But I use the above for the sake
> of simplicity.
> 
> I want to split the conent of this input file so that
> each segment (record) of text (blank line as record separator)
> will be copied to text files, like this :
> 
> ./QLOCAL/Q1 <-- the define statement as file content
> ./QLOCAL/Q2
> ./QALIAS/A1
> ./QALIAS/A2
> 
> That is, the subfolder is the name (QLOCAL or QALIAS) after
> the word DEFINE. While the filename corresponds to the string
> surrounded by ('<as_filename>').
> 
> Appreciate your help.

If the directories are already existing

  awk -v q="'" -v RS= '{ split($3,f,q); print > $2"/"f[2] }'

If awk shall also create the directories

  awk -v q="'" -v RS= '
      { split($3,f,q); system("mkdir -p "$2); print > $2"/"f[2] }'


Janis
0
Reply Janis 8/12/2010 11:17:59 PM


On Aug 12, 4:17=A0pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:

> If the directories are already existing
>
> =A0 awk -v q=3D"'" -v RS=3D '{ split($3,f,q); print > $2"/"f[2] }'
>
> If awk shall also create the directories
>
> =A0 awk -v q=3D"'" -v RS=3D '
> =A0 =A0 =A0 { split($3,f,q); system("mkdir -p "$2); print > $2"/"f[2] }'
>

Cool.
Thanks
0
Reply Harry 8/13/2010 2:14:49 AM

2 Replies
1574 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/19/2012 6:18:48 PM


Reply: