The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sort files by date itik Linux 1 06-02-2008 07:25 PM
sort files by date, delete oldest, if total size bigger than scarfake Shell Programming and Scripting 2 05-21-2008 09:02 AM
loop through the directory for files and sort by date and process the first file dsdev_123 AIX 1 01-30-2008 05:31 PM
Subtracting date / timestamps roadcyclist Shell Programming and Scripting 0 06-21-2006 03:51 PM
Renaming files to have date/time in filename wayneb UNIX for Dummies Questions & Answers 5 01-19-2005 10:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-04-2006
Chindhu Chindhu is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
Question Sort files by Date-timestamps available in filename & pick the sortedfiles one by one

Hi,

I am new to Unix shell scripting. Can you please help me with this immediate requirement to code.. The requirement is as given below.

In a directory say Y, I have files like this.

PP_100000_28062006_122731_746.dat
PP_100000_28062006_122731_745.dat
PP_100000_28062006_122734_745.dat
PP_100000_28062006_122732_745.dat
PP_100000_28062006_122801_745.dat

QQ_100001_28062006_122733_745.dat
QQ_100001_28062006_122731_745.dat

RR_100002_28062006_122731_745.dat
RR_100002_28062006_122732_745.dat
RR_100002_28062006_122729_745.dat

format: <type>_<somesequence>_DDMMYYYY_HHMMSS_<organisationID>

Now, I will have an input parameter coming in as 'Directory path' (path to Y) and type (can be PP, QQ or RR. If type is NULL, means I need to process all the 3 types)

Assuming we get type as PP

Now I need to pick up files of this type. We can see there are 5 files matching this string.
Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it, and pick up the file with the lowest date timestamp in its name, first - and process it.
As you see I have 2 files with same timestamp here - PP_100000_28062006_122731_746.dat and PP_100000_28062006_122731_745.dat. In this case I have to process it in ascending order of <organisationID>. ie, PP_100000_28062006_122731_745.dat first, followed by PP_100000_28062006_122731_746.dat

Once processed, I have to pick the next lowest date time stamp file for processing.

I will be archiving successfully processed files in another directory, and renaming failed files to say FAILED (suffix) so that it wont be picked up again.

Any sample code/ pointers/ suggestions would be of great help.

Thanks a lot in advance,
Chindhu
  #2 (permalink)  
Old 07-04-2006
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
Sorting the files is simple if you can keep your head with the sort key definitions (man sort):


Code:
...
patt=$1         ;# get pattern to match
# do some validation on pattern (your code)
#
# loop through the files in order
ls $patt*| sort -t'_' -k3.5,3.8 -k3.3,3.4 -k3.1,3.2 -k4 -k5| while read a
do
  print  "processing $a"
# process each file here (your code)

# check to see if it worked
  if [ (some condition to test whether it worked or not ) ]; then
    print "Processed successfully"
    mv $a (processed files directory)
  else
    print "Processing failed"
    mv $a FAILED$a
  fi
done
...

hope this helps

cheers
  #3 (permalink)  
Old 07-04-2006
Chindhu Chindhu is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 2
Hi Thestevew,

Thanks for your reply and sample code.
I will try this out.
I was actually trying out some logic with grep, cut and sort.

Thanks,
Chindhu
  #4 (permalink)  
Old 08-10-2007
srikanthgr1 srikanthgr1 is offline
Registered User
  
 

Join Date: Apr 2007
Location: Bangalore
Posts: 17
Quote:
Originally Posted by thestevew View Post
Sorting the files is simple if you can keep your head with the sort key definitions (man sort):


Code:
...
patt=$1         ;# get pattern to match
# do some validation on pattern (your code)
#
# loop through the files in order
ls $patt*| sort -t'_' -k3.5,3.8 -k3.3,3.4 -k3.1,3.2 -k4 -k5| while read a
do
  print  "processing $a"
# process each file here (your code)

# check to see if it worked
  if [ (some condition to test whether it worked or not ) ]; then
    print "Processed successfully"
    mv $a (processed files directory)
  else
    print "Processing failed"
    mv $a FAILED$a
  fi
done
...

hope this helps

cheers
Hi Steve,

I have a smiliar requirement
But I was unable to figure out the sort command used .
If u can help me on that that will be great.


The are many files in a folder Which are of the below types

Ack_to_MDS_20070809141159.xml
Ack_to_MDS_20070809141157.xml
Ack_to_MDS_20070809141155.xml
Ack_to_MDS_20070809141151.xml
Ack_to_MDS_20070809141149.xml
Ack_to_MDS_20070809141148.xml
.
.
.
.

The files are of format Ack_to_MDS_YYYYMMDDHHMMSS.xml

The Content of the files are like this

Later I need to cat the files based on the timestamps present in the file format i.e Ack_to_MDS_YYYYMMDDHHMMSS.xml.
The Sort order is the oldest file contents needs to be at the top and the Latest file at the end.

i.e. If theese are the files

Ack_to_MDS_20070809141159.xml
Ack_to_MDS_20070809141157.xml
Ack_to_MDS_20070809141155.xml
Ack_to_MDS_20070809141151.xml
Ack_to_MDS_20070809141149.xml
Ack_to_MDS_20070809141148.xml


Then The Sorted Order is
Ack_to_MDS_20070809141148
Ack_to_MDS_20070809141149
Ack_to_MDS_20070809141151
Ack_to_MDS_20070809141155
Ack_to_MDS_20070809141157
Ack_to_MDS_20070809141159

Later The concatenation order is also as the above .

Then Append the Tags <ACKNOWLEDGEMENTS> and </ACKNOWLEDGEMENTS> to the first line and last line of the concatenated file.

Please Gurus
Need Help and Advice.

Thanks & Regards
Srikanth GR
  #5 (permalink)  
Old 08-10-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Here you have , two ways :

If you want to keep the result of the concat process in 1 file:


Code:
ls Ack*|xargs -i ksh -c '(echo "<ACKNOWLEDGEMENTS>";cat {};echo "</ACKNOWLEDGEMENTS>") ' >file

If you want to change the source files:

Code:
ls Ack*|xargs -i ksh -c '(echo "<ACKNOWLEDGEMENTS>";cat {};echo "</ACKNOWLEDGEMENTS>") >{}.tmp;mv {}.tmp {}'

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 01:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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