Sort files by Date-timestamps available in filename & pick the sortedfiles one by one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort files by Date-timestamps available in filename & pick the sortedfiles one by one
# 1  
Old 07-04-2006
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  
Old 07-04-2006
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  
Old 07-04-2006
Hi Thestevew,

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

Thanks,
Chindhu
# 4  
Old 08-10-2007
Quote:
Originally Posted by thestevew
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  
Old 08-10-2007
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 {}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

2. Shell Programming and Scripting

[Solved] Modifying/Removing Timestamps from a filename

So given filenames of varying lengths, I was wondering how I would remove or modify appended timestamps of the current date DD-MM-YY. So say: test_DD-MM-YY.txt coolbeans_DD-MM-YY.pdf And what I expect the output to be: test.txt coolbeans.pdf Thanks :) (2 Replies)
Discussion started by: sodaboyz
2 Replies

3. Shell Programming and Scripting

Sort log files based on numeric value in the filename

Hi, I have a list of log files as follows: name_date_0001_ID0.log name_date_0001_ID2.log name_date_0001_ID1.log name_date_0002_ID2.log name_date_0004_ID0.log name_date_0005_ID0.log name_date_0021_ID0.log name_date_0025_ID0.log .......................................... (4 Replies)
Discussion started by: alex2005
4 Replies

4. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

5. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

6. Shell Programming and Scripting

Sort files by date in filename

Hi, I am a newbie to shell programming and I need some help in sorting a list of files in ascending order of date in the filenames. The file format is always : IGL01_AC_D_<YYYYMMDD>_N01_01 For example, in a directory MyDirectory I have the following files: IGL01_AC_D_20110712_N01_01.dat... (11 Replies)
Discussion started by: Yuggy
11 Replies

7. UNIX for Dummies Questions & Answers

sort files by numeric filename

dear all, i have .dat files named as: 34.dat 2.dat 16.dat 107.dat i would like to sort them by their filenames as: 2.dat 16.dat 34.dat 107.dat i have tried numerous combinations of sort and ls command (in vain) to obtain : 107.dat 16.dat 2.dat 34.dat (1 Reply)
Discussion started by: chen.xiao.po
1 Replies

8. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

9. Shell Programming and Scripting

Getting all the .gz files between Two Date/TimeStamps

Hi, I am trying to write a script to ftp and get all the files between two date/time stamps from a archive directory. I have sent an attatchment of my archive directory. With the script I intend to get files for ex: between request.log.2008-08-22-03-53-49.gz &... (3 Replies)
Discussion started by: openspark
3 Replies

10. Shell Programming and Scripting

sort & match multiple files

Hi, I have some question and need some guidance how to sort and match multiple files. 1. all the data in the files are numbers e.g. 1234567 1584752 2563156 2. each sorted file have their own ouput. e.g. test.csv -> test_sorted.csv 3. Then, I need to match all... (4 Replies)
Discussion started by: nazri76
4 Replies
Login or Register to Ask a Question