The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep latest file based on date. lweegp Shell Programming and Scripting 12 04-03-2009 03:10 AM
Reading a file using sh with spaces in filename jekl5 Shell Programming and Scripting 4 02-29-2008 10:18 AM
Loop through files in dir, omit file with latest date stringzz Shell Programming and Scripting 2 12-04-2007 02:04 PM
Grabing Date from filename and adding to the end of each line in the file. rkumar28 Shell Programming and Scripting 1 05-01-2007 08:25 PM
Copying files with the latest date shashi_kiran_v UNIX for Dummies Questions & Answers 34 08-02-2005 12:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-25-2008
prsshini prsshini is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 20
get the latest file by reading the date in the filename.

Hi,
I grep for a pattern in a list of files.

"grep -i -l $pattern *.datx*"

it may give me n number of files.
say for eg, it gives me 2 files.
lock_eicu_20071228_00000000.dat_20071228_05343100
lock_eicu_20080501_00000000.dat_20080501_05343900

out of these 2 files I need to get the latest file according to the date present after the .dat extn. As 20071228 is older than 20080501, i need to get the second file.

so in the above eg, i need to get the filename
lock_eicu_20080501_00000000.dat_20080501_05343900
can u please help.
  #2 (permalink)  
Old 11-25-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Assuming all the names are in the same format:

Code:
awk -F_ 'm<$5{m=$5;f=$0}END{print f}'
  #3 (permalink)  
Old 11-25-2008
prsshini prsshini is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 20
Many Thanks Frank, it works perfect.
  #4 (permalink)  
Old 11-25-2008
prsshini prsshini is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 20
Hi frank, one more help please,
I do below to list my files in chronological order.
ls -ltr *.datx* | tr -s ' ' | cut -d' ' -f9

it gives me list of files like below.

lock_eicu_20081120_00000000.datx_20081120_05315400
lock_eicu_20081118_00000000.datx_20081118_06303100
lock_eicu_20081111_00000001.datx_20081111_15531300
lock_eicu_20081111_00000000.datx_20081111_06335300
lock_eicu_20081030_00000000.datx_20081030_05342900
lock_eicu_20081016_00001111.datx_20081016_15525600
lock_eicu_20081016_00000000.datx_20081016_06350500
lock_eicu_20081001_00000000.datx_20081001_05344100
lock_eicu_20080912_00000000.datx_20080912_05305200
lock_eicu_20080904_00000000.datx_20080904_05335500
lock_eicu_20080822_00000000.datx_20080822_05344200
lock_eicu_20080812_00000000.datx_20080812_06331200
lock_eicu_20080808_00000000.datx_20080808_05345200
lock_eicu_20080806_00000000.datx_20080806_06343800
lock_eicu_20080802_00000000.datx_20080804_05332000
lock_eicu_20080718_00000000.datx_20080718_06533100
lock_eicu_20080606_00000000.datx_20080606_05340600
lock_eicu_20080603_00000000.datx_20080603_06345300
lock_eicu_20080531_00000000.datx_20080602_05304300
lock_eicu_20080529_00000000.datx_20080529_06330500
lock_eicu_20080501_00000000.datx_20080501_05343900
lock_eicu_20080429_00000000.datx_20080429_06311200
lock_eicu_20071228_00000000.datx_20071228_05343100

how do i sort the files again chronologically as per the date found just after .datx?
i mean in the list if u see "lock_eicu_20071228_00000000.datx_20071228_05343100" is the oldest file as it has date 20071228 after datx. so i need to list this file first and then sort so on.

Please help. Thanks again
  #5 (permalink)  
Old 11-25-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Try:

Code:
sort -n -t_ -k5
  #6 (permalink)  
Old 11-25-2008
prsshini prsshini is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 20
Thanks a lot Frank. it works perfect.
  #7 (permalink)  
Old 11-25-2008
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
hey Franklin,

care to explain the breakdown of the awk statement?

thanks
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0