Copying Content of a Newest file

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
rkumar28
Participant
Posts: 43
Joined: Tue Mar 30, 2004 9:39 am

Copying Content of a Newest file

Post by rkumar28 »

Hi,
I have a situation, I get ONE file everyday through FTP in my unix box. That file name varies depending on the source that is sending the file to us. I have created a generic file called LOAD.TXT file. DataStage points to this file. Everyday in Unix I copy the content of the newly ftped file to LOAD.TXT. I use this command cat ftped filename > LOAD.txt to copy the content.

I am trying to automate this process. I am trying to write a shell script that picks up the latest file in the directory(i.e. newest file that comes in) and pass the content of that file to LOAD.TXT. This script will be called by DataStage on the "Before job Routine".

I am little new to Unix and need some help/advice on the script. I tried something like:

var1=ls -1t | head -1 --> This line picks up the newest file in the directory.
cat $var1>LOAD.txt
I error out on the first line of my script.
Also I tried something like ls -1t | head -1 | cat >load.txt. But I am not getting the desired result.

Is their a way to pass the output of the line below to a variable var1.
ls -1t | head -1. Or Is their a better way to do through DataStage.

I will really appreciate any help in this regards.

Thanks
Raj
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Just make it a single command, using backquotes to capture the output of the pipeline.

Code: Select all

cat `ls -1t | head -1` > load.txt
Why cat? Surely a cp command would be more efficient.

Code: Select all

cp `ls -1t | head -1` load.txt
Maybe you want to remove load.txt first. No problem.

Code: Select all

rm -f load.txt ; cp `ls -1t | head -1` load.txt
You probably also want to cater for the case that there are no files in the directory, in which case the cp command would fail.

Code: Select all

rm -f load.txt ; ls -1t * && cp `ls -1t | head -1` load.txt
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
tonystark622
Premium Member
Premium Member
Posts: 483
Joined: Thu Jun 12, 2003 4:47 pm
Location: St. Louis, Missouri USA

Post by tonystark622 »

Raj,

I suspect that the first command is failing because you are doing a "long" format ls (ls -lt). Instead, try: ls -t . This should only give you the filename and not the information about the file.

Good Luck!

Tony
WoMaWil
Participant
Posts: 482
Joined: Thu Mar 13, 2003 7:17 am
Location: Amsterdam

Post by WoMaWil »

Hi Tony,

Ray and Raj did it write because they used 1 (one) and not l (lambda).

Wolfgang
tonystark622
Premium Member
Premium Member
Posts: 483
Joined: Thu Jun 12, 2003 4:47 pm
Location: St. Louis, Missouri USA

Post by tonystark622 »

:oops: Oops. I'm embarassed. I didn't read that carefully. Thanks for the correction. :D

Tony
Post Reply