If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > I need your help.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-15-06, 21:11
LEO111 LEO111 is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Wink I need your help.

Hi
I have a multiple files in the same directory . The only different between these file is the date stamp.
For example : a_20060308.dat
a_20060309.dat

without the datestamp, these files will have the same file name (which is a)
and of course, these two have different created date.

I just want to pickup the lastest file and mv that latest file to different location. Does anyone knows or have any idea how to get this using korn shell or awk/sed ...? Thank you so much in advance.
Reply With Quote
  #2 (permalink)  
Old 09-16-06, 06:18
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
If you want to do it by timestamp you can write:
Quote:
mv $(ls -ltr a_*.dat | awk 'END{print $9}') somelocation
where the '$9' in the awk statement represents the ninth (last) field in the ls -l outputline. This is the filename on many UNIX / Linux systems. By using ls -ltr the listing is sorted on t(ime)r(eversely).

Regards

BTW. This is Linux syntax, if it doesn't work on your system you always can use `` instead of $().
Reply With Quote
  #3 (permalink)  
Old 09-16-06, 18:45
LEO111 LEO111 is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Re: I need Your help

TYVELEYN

Thank you so much for your help regarding this matter. I'll try this approach and will let you updated. Again, Thank you and Have a nice weekends.

Best Regards,
LEO111.
Reply With Quote
  #4 (permalink)  
Old 09-16-06, 18:49
LEO111 LEO111 is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
TYVELLEYN
The files name is only example... The problem is I have lot of file name and I don't want to specify in my scripts what is the name of the files. Whatever files are there , I just want to pick up the lastest one if duplicate. So I can not hard coded the file name there. Is there a way to get rid of the datestamp first then apply the ls -ltr command to it to get the lastest file. Thank you.
Reply With Quote
  #5 (permalink)  
Old 09-18-06, 03:08
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
e.g.
latestfile=$(ls -A1t /tmp |head -1)

What do you mean by "if duplicate"
There can't be duplicate filenames in a directory
Do you want to sort on file name?
Just use ls -A1r instead
Reply With Quote
  #6 (permalink)  
Old 09-18-06, 04:33
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Maybe this is what you're looking for:
Code:
#!/usr/bin/ksh                                                                
                                                                              
for i in $(ls -tr *[12][09][0-9][0-9][01][0-9][0-3][0-9]*); do
    cp $i somelocation/$(echo $i | sed 's/^\(.*[^0-9]\)[12][09][0-9][0-9][01][0-9][0-3][0-9]\([^0-9].*\)$/\1\2/')
done
This is a very basic algorithm for detecting files with a datevalue in their names (yyyymmdd). It cuts out the datevalue from every filename found and writes the original files to a file with the stripped name.
E.g. every a_YYYYMMDD.dat file will be written to 'a_.dat' and every b_YYYYMMDD.dat file to 'b_.dat'. Because the filenames are fetched with 'ls -tr' (adopted from pdreyer, didn't know it would work without the long list...) the files are reversely sorted by timestamp and thus overwrite the previous (= older) file with the target filename.

Regards

Last edited by Tyveleyn; 09-18-06 at 04:37.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On