Create multiple files without looping

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
reachmexyz
Premium Member
Premium Member
Posts: 296
Joined: Sun Nov 16, 2008 7:41 pm

Create multiple files without looping

Post by reachmexyz »

Hello all

I have data like

col1 col2 col3

aaa 21 22
aaa 31 23
bbb 12 12
ccc 32 12
bbb 12 34

Now i have to generate separate files for each distinct value of col1.
In this case i will generate 3 files.
1st file will be like
aaa 21 22
aaa 31 23

2nd file will be like
bbb 12 12
bbb 12 34

3rd file will be like
ccc 32 12.

This task is easy if the distinct values in col1 are fixed. But in my case once i run the job, distinct values in col1 can be 12,20,3,4 .. i dont know.
Its all dynamic. Still we can do this using the looping activity in Datastage sequence. we can loop the job for each distinct value of col1. But i do not want to do using looping activity. I want to run the process once and multiple files should be generated each having one distinct value of col1.
Is this possible? Can we use any of the DataStage stages and implement this? Please reply.
anbu
Premium Member
Premium Member
Posts: 596
Joined: Sat Feb 18, 2006 2:25 am
Location: india

Post by anbu »

Unix solution

Code: Select all

awk ' { print > $1 } ' file
You are the creator of your destiny - Swami Vivekananda
reachmexyz
Premium Member
Premium Member
Posts: 296
Joined: Sun Nov 16, 2008 7:41 pm

Post by reachmexyz »

anbu wrote:Unix solution

Code: Select all

awk ' { print > $1 } ' file
I did not understand. Can you please elaborate your response.
truenorth
Participant
Posts: 139
Joined: Mon Jan 18, 2010 4:59 pm
Location: San Antonio

Post by truenorth »

In a Sort stage, turn key change column to True. This key change column stores 1 if the key value of the current row is not equal to the previous row's value or 0 if the key value of the current row is equal to the previous row's value. So whenever the key change column is 1, write to a new file.

As a newbie myself, I have not figured out how to write to a new file based on a condition. Maybe someone else can pitch in here.
Todd Ramirez
Sr Consultant, Data Quality
San Antonio TX
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

A Server job writing to a Folder stage can handle that. As (technically) can the XML Output stage, it doesn't even need to be XML for the Trigger Column there to help you out.
-craig

"You can never have too many knives" -- Logan Nine Fingers
anbu
Premium Member
Premium Member
Posts: 596
Joined: Sat Feb 18, 2006 2:25 am
Location: india

Post by anbu »

Code: Select all

$ ls
file
$ cat file
aaa 21 22
aaa 31 23
bbb 12 12
ccc 32 12
bbb 12 34
$ awk ' { print > $1 } ' file
$ ls
aaa  bbb  ccc  file
$ cat aaa
aaa 21 22
aaa 31 23
$ cat bbb
bbb 12 12
bbb 12 34
$ cat ccc
ccc 32 12
print > $1 Prints line to filename based on the value in first field
You are the creator of your destiny - Swami Vivekananda
reachmexyz
Premium Member
Premium Member
Posts: 296
Joined: Sun Nov 16, 2008 7:41 pm

Post by reachmexyz »

anbu wrote:

Code: Select all

$ ls
file
$ cat file
aaa 21 22
aaa 31 23
bbb 12 12
ccc 32 12
bbb 12 34
$ awk ' { print > $1 } ' file
$ ls
aaa  bbb  ccc  file
$ cat aaa
aaa 21 22
aaa 31 23
$ cat bbb
bbb 12 12
bbb 12 34
$ cat ccc
ccc 32 12
print > $1 Prints line to filename based on the value in first field

This didnt work. It created multiple files namely
aaa 21 22
aaa 31 23
bbb 12 12
bbb 12 34
ccc 32 12

What happened is each record was written t o a different file and the file name is same as record.
anbu
Premium Member
Premium Member
Posts: 596
Joined: Sat Feb 18, 2006 2:25 am
Location: india

Post by anbu »

What is the field delimiter in your input file?
You are the creator of your destiny - Swami Vivekananda
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Use a server job with a Folder stage.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

What! Server job! No freak'n way!

<wink>
-craig

"You can never have too many knives" -- Logan Nine Fingers
jgreve
Premium Member
Premium Member
Posts: 107
Joined: Mon Sep 25, 2006 4:25 pm

nice - awk example

Post by jgreve »

anbu wrote:Unix solution

Code: Select all

awk ' { print > $1 } ' file
nice example, anbu 8)

Also a Windows solution, since DataStage includes MKS, which also has an awk.
prasad111
Premium Member
Premium Member
Posts: 173
Joined: Fri May 19, 2006 10:53 am

Post by prasad111 »

You can use external target stage ... example here viewtopic.php?t=126634&highlight=external+target
Post Reply