Visit Our UNIX and Linux User Community


Sort By Date and Time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort By Date and Time
# 8  
Old 01-02-2008
Hi Smiling Dragon,

I am using your code now.

Code:
ls -1 * | awk 'BEGIN { FS="_" } { print $2,$3,$4,$1}' | sort -n | awk 'BEGIN { OFS="_" } { print $4,$1,$2,$3}'

But now i have another problem, some of the filenames come in the form of " zzz_071128_144121_data_interim ", how can i sort it out by modifying your code? Example below

Input:
abc_071128_144121_data
xxx_071128_123524_data
fff_071128_122837_data
jjj_071129_010544_data
qqq_071129_002603_data
hhh_071128_120728_data
bbb_071128_113329_data
zzz_071128_163017_data_interim


Ouput:
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
zzz_071128_163017_data_interim
qqq_071129_002603_data
jjj_071129_010544_data
# 9  
Old 01-02-2008
Quote:
Originally Posted by fpmurphy
The following works for the dataset provided:

Code:
sort -t'_' -k2 -k3  datasetfile

Have not tested it on boundary cases.
Why don't you use the above code - its simple, efficient, easy to maintain & isn't affected by other fields.
# 10  
Old 01-02-2008
Hi rikxik,

Tried the code but encounter some errors. Can you give me some advice?

$ sort -t'_' -k2 -k3 *
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -l |sort -t'_' -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
# 11  
Old 01-02-2008
Works fine for me:

Sample:
Code:
$ ls -ltr
total 0
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 xxx_071128_123524_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 qqq_071129_002603_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 jjj_071129_010544_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 hhh_071128_120728_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 fff_071128_122837_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 bbb_071128_113329_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 abc_071128_144121_data

Output:
Code:
$ ls -1 |sort -t'_' -k2 -k3
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data

Do you know how '|' works or I am getting blind here?
# 12  
Old 01-03-2008
Hi rikxik,

I am using solaris by the way.
This code's not working for me. Smilie
Error message is telling me to input an argument for " k ". Do you have any idea on that ?

$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim

$ ls -1 |sort -t'_' -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -1 |sort -t"_" -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]
# 13  
Old 01-03-2008
And I'm using sunOS - so shouldn't be that different:

Quote:
$ uname -a
SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
Try this:

Code:
$ ls -1 |sort -t'_' -k2,2 -k3,3
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data

This syntax should address your sort's crib.

HTH
# 14  
Old 01-03-2008
Hi rikxik,

It still doesn't work. Can you help ?

$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim

$ ls -1 |sort -t'_' -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -1 |sort -t"_" -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

Previous Thread | Next Thread
Test Your Knowledge in Computers #703
Difficulty: Medium
Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort date time in ascending order

Hi, i had a data block (coming from pipe from other codes) as: H YF_CO.dat 77164 11/17/2013 04:00:02 731374590.96 1 1 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77164 11/17/2013 04:00:07 731374590.96 1 4 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77178 ... (5 Replies)
Discussion started by: pr5439
5 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

4. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

How to Sort by Date and Time?

Hi, The input file is as follows, 22.06.2012 17:58:38 CPUser: xxxxxxx, billedAfterStatus: Active 13.07.2012 08:46:15 CPUser: xxxxxxx, billedAfterStatus: Active 20.07.2012 08:56:24 CPUser: xxxxxxx, billedAfterStatus: Active 20.03.2012 08:56:24 CPUser: xxxxxxx, billedAfterStatus: Active... (16 Replies)
Discussion started by: nanthagopal
16 Replies

6. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

7. Solaris

modifying date and time and time zone on solaris 5.10 with (redundant server) veritas

I have a cluster of two Solaris server (veritas cluster). one working and the other is standby I am going to change the date on them , and am looking for a secure solution as it is giving an important service. my opinion is that the active one doesn't need to be restarted (if I don't change the... (1 Reply)
Discussion started by: barry1946
1 Replies

8. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

9. Shell Programming and Scripting

shell script to sort entries in a file by date and time

Hello All, Need a shell script to sort entries in a file by date and time. Below are the entries in the file, i need to sort it first by the date and then time Note :- Date is in MM/DD/YY format and date comes as the 6th & time comes on 7th coloumns respectively. 150 pbnawldb001-b... (10 Replies)
Discussion started by: ajiwww
10 Replies

10. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

Featured Tech Videos