Sorting list of files per date column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting list of files per date column
# 1  
Old 11-06-2008
Sorting list of files per date column

Hi all,

I have a pecular issue in sorting these files (not an ls -lrt) in Solaris environment.

All the below files are modified on November 4th, but I want to sort these files as per date (eg: 01May07_1623 = ddmmmyy_hhmm)

Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Mar08_1504.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Sep07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.08Nov07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22973.22Jan08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.24Apr07_1757.gpg
Nov 4 18:27 SONYELEC00.GI22973.26Jun08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22974.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22974.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22974.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22974.24Apr07_1757.gpg

Appreciate your quick support

Thanks,
Shiva
shivadba@hotmail.com
Smilie

Last edited by shivaastrogun; 11-06-2008 at 12:53 PM..
# 2  
Old 11-06-2008
Hello,
You need to convert the date string to epoch time (seconds since 1970). Therfore you first have to format the string to meet the requirements of your converter. On Linux you could use "date -d" (date MAN Page) but perl or awk are much more flexible. I dont have the time to provide a complete script, sorry.
# 3  
Old 11-06-2008
sorting

Quote:
Originally Posted by demwz
Hello,
You need to convert the date string to epoch time (seconds since 1970). Therfore you first have to format the string to meet the requirements of your converter. On Linux you could use "date -d" (date MAN Page) but perl or awk are much more flexible. I dont have the time to provide a complete script, sorry.

please provide the script to sort files as requested
# 4  
Old 11-06-2008
Quote:
Originally Posted by shivaastrogun
please provide the script to sort files as requested
nobody is writing scripts for you! this is your job, isn't it? start with the script yourself and if you have problems at some point, ask for help...

greets,
DN2
# 5  
Old 11-06-2008
I have tried using awk, but I am not getting. As I am new to shell scripting, I need your help

I have tried below command but it is picking entire 6th field, I need to sort part of date field
ls -lrt | awk '{print 6}'

Thanks
S2
# 6  
Old 11-06-2008
Hammer & Screwdriver a quick primer on sort

Take a look at the following (and yes, the cat is not needed but here to show the sort all contained in one stream)
the -t set a delimiter of .
the -k are the key fields
and 3.6,3.7 means take the 3rd field and consider character positions 6 thru 7 as the sort request.

Perhaps this can get you started.

A thought... use awk first to read the month field, and then append to the end of each line a numeric for the month - ie append "|05" for May. Then do your 2nd level sort on that field. Before outputting results, cut out the ending "|05" and other month numbers.


Code:
> cat file40
Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Mar08_1504.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Sep07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.08Nov07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22973.22Jan08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.24Apr07_1757.gpg
Nov 4 18:27 SONYELEC00.GI22973.26Jun08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22974.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22974.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22974.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22974.24Apr07_1757.gpg
> cat file40 | sort -t"." -k 3.6,3.7 -k 3.3,3.5 -k 3.1,3.2
Nov 4 18:27 SONYELEC00.GI22973.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22974.19Apr07_1733.gpg
Nov 4 18:27 SONYELEC00.GI22973.24Apr07_1757.gpg
Nov 4 18:27 SONYELEC00.GI22974.24Apr07_1757.gpg
Nov 4 18:27 SONYELEC00.GI22973.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22974.13Aug07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22974.01May07_1623.gpg
Nov 4 18:27 SONYELEC00.GI22973.08Nov07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Sep07_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.22Jan08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.26Jun08_1900.gpg
Nov 4 18:27 SONYELEC00.GI22973.06Mar08_1504.gpg

# 7  
Old 11-06-2008
help need to sort files

Hi,

When I tried to execute below script to sort datefield (eg: 13Aug07_1900), it is not working

cat amex1025 | sort -t"." -k 9.6,9.7 -k 9.3,9.5 -k 9.1,9.2

our file name is: amex1025

-rwxrwxrwx 1 stuprd stuprd 2604 Nov 4 18:27 SONYELEC00.GI22976.13Aug07_1900.gpg
-rwxrwxrwx 1 stuprd stuprd 154129 Nov 4 18:27 SONYELEC00.GI22976.19Apr07_1733.gpg
-rwxrwxrwx 1 stuprd stuprd 146385 Nov 4 18:27 SONYELEC00.GI22976.26Jun08_1900.gpg
-rwxrwxrwx 1 stuprd stuprd 2605 Nov 4 18:27 SONYELEC00.GI22977.13Aug07_1900.gpg
-rwxrwxrwx 1 stuprd stuprd 95605 Nov 4 18:27 SONYELEC00.GI22977.19Apr07_1733.gpg
-rwxrwxrwx 1 stuprd stuprd 147299 Nov 4 18:27 SONYELEC00.GI22977.26Jun08_1900.gpg

please advice5

Thanks
S2
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

2. UNIX for Dummies Questions & Answers

Date column validation in files

HI, i have to do some file validations on which i am receving daily.could any one please let me know how we can able to do validations on below scenarios 1) On Date column Ex If i am receving date in format of YYYYMMDD---- 20141118 --- here i have to check the date value 20141142 if the... (7 Replies)
Discussion started by: bhaskar v
7 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Creating a two column list of date pairs form a single column list

Hi all, looking for some help here. I'm what you'd call a dirty programmer. my shell scripts might be ugly, but they (usually) function... Say I have a single column text file with a list of dates (yyyymmdd) that represent the elevation of a point on that date (I work with land subsidence, so... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

5. Shell Programming and Scripting

Finding files and sorting by date (find | awk)

I am wanting to search a directory tree and return files that are older than a specified datetime. So far straight forward with find, now I want to sort in date order and format the output. So far I have this, but is not working and there is a problem with "." in the file and/or path names. ... (2 Replies)
Discussion started by: larry2311
2 Replies

6. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

7. Shell Programming and Scripting

[SOLVED] Sorting into new files based on column

Hi, I have the following file, I need to sort it based on a column and write to different output files based on this column request_guid iso_country_cd address_data response_time 32895901-d17f-414c-ac93-3e7e0f5ec240 AND BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND;... (1 Reply)
Discussion started by: ramky79
1 Replies

8. UNIX for Advanced & Expert Users

Help with sorting files according to modification date

Hi, I was very surprised to not be able to find an answer to this question despite my best efforts in Google and elsewhere. Maybe it's a good thing as it forced me to finally become a member in this great forum that i use frequently. Ok my question: I want to be able to sort files inside a... (3 Replies)
Discussion started by: stavros
3 Replies

9. UNIX for Dummies Questions & Answers

Get the list of files for a given date

Hi, the command "ls -l" gives the list of files of a directory with details such as date and size. Is there a way to restrict this list to see only files created on a given date? Suppose I want to list only the files created today, what is the command to type? Thanks (3 Replies)
Discussion started by: vanagreg
3 Replies

10. Shell Programming and Scripting

Sorting Files by date and moving files in date order

I need to build a k shell script that will sort files in a directory where files appear like this "XXXX_2008021213.DAT. I need to sort by date in the filename and then move files by individual date to a working folder. concatenate the files in the working folder then start a process once... (2 Replies)
Discussion started by: rebel64
2 Replies
Login or Register to Ask a Question