sort file on timestamp and a alphabetic code in it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sort file on timestamp and a alphabetic code in it
# 1  
Old 10-03-2008
sort file on timestamp and a alphabetic code in it

Hi,

I have files like


ER08EVENTDC080911232324.txt
ER08EVENTCD080911232323.txt
ER08EVENTCD080910222222.txt
ER08EVENTCD080910130245.txt
ER08EVENTAA080910130245.txt
ER08EVENTAA080910232000.txt
ER08EVENTAA080911015005.txt
ER08EVENTAA080910130150.txt
ER08EVENTAA080910232010.txt
ER08EVENTAA080910224550.txt

I want to sort the files first based on 2 byte alphabetic code (so that all CD's together all AA's together all DC's together). Next within that i want to sort on the basis of timestamp.

So file format is like ERyyEVENTlayymmddhhmmss.txt

where la=2 byte alphabetic code and rest is timestamp.

I tried using
ls -lrt *EVENT*|sort -r -k1.10,1.11 -k1.12,1.13 -k1.14,1.15 -k1.16,1.17 -k1.18,1.19 -k1.20,1.21 -k1.22,1.23

but my output is not proper the AA code files are not getting sorted properly.

Can anyone please help?

Thanks,
H

# 2  
Old 10-03-2008
Hammer & Screwdriver What about this simplified sort command?

Code:
> cat file9
ER08EVENTDC080911232324.txt
ER08EVENTCD080911232323.txt
ER08EVENTCD080910222222.txt
ER08EVENTCD080910130245.txt
ER08EVENTAA080910130245.txt
ER08EVENTAA080910232000.txt
ER08EVENTAA080911015005.txt
ER08EVENTAA080910130150.txt
ER08EVENTAA080910232010.txt
ER08EVENTAA080910224550.txt

> sort -k1.10,23 file9
ER08EVENTAA080910130150.txt
ER08EVENTAA080910130245.txt
ER08EVENTAA080910224550.txt
ER08EVENTAA080910232000.txt
ER08EVENTAA080910232010.txt
ER08EVENTAA080911015005.txt
ER08EVENTCD080910130245.txt
ER08EVENTCD080910222222.txt
ER08EVENTCD080911232323.txt
ER08EVENTDC080911232324.txt

# 3  
Old 10-03-2008
thanks.. it was that simple.. I was unnecessary complicating things Smilie
# 4  
Old 10-03-2008
This should sufficient:

Code:
sort -k 9

# 5  
Old 10-03-2008
Why is output different in case i do ls -lrt first and not take the list of files in a file and then sort the file..
Below output is different as mentioned above.
ls -lrt |sort -k1.10,23
total 80
-rwx------ 1 dsadm dstage 81 Oct 03 10:53 ER08EVENTAA080910224550.txt
-rwx------ 1 dsadm dstage 81 Oct 03 10:56 ER08EVENTAA080910232010.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:38 ER08EVENTAA080910130150.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:47 ER08EVENTAA080911015005.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:51 ER08EVENTAA080910232000.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:06 ER08EVENTAA080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:22 ER08EVENTCD080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:23 ER08EVENTCD080910222222.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:34 ER08EVENTCD080911232323.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:35 ER08EVENTDC080911232324.txt

ls -lrt |sort -k 9 is working
# 6  
Old 10-03-2008
Without recreating your filestructure, I copied the output of your ls -ltr to file9a
Code:
> cat file9a
-rwx------ 1 dsadm dstage 81 Oct 03 10:53 ER08EVENTAA080910224550.txt
-rwx------ 1 dsadm dstage 81 Oct 03 10:56 ER08EVENTAA080910232010.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:38 ER08EVENTAA080910130150.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:47 ER08EVENTAA080911015005.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:51 ER08EVENTAA080910232000.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:06 ER08EVENTAA080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:22 ER08EVENTCD080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:23 ER08EVENTCD080910222222.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:34 ER08EVENTCD080911232323.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:35 ER08EVENTDC080911232324.txt

Code:
> cut -d" " -f9 file9a
ER08EVENTAA080910224550.txt
ER08EVENTAA080910232010.txt
ER08EVENTAA080910130150.txt
ER08EVENTAA080911015005.txt
ER08EVENTAA080910232000.txt
ER08EVENTAA080910130245.txt
ER08EVENTCD080910130245.txt
ER08EVENTCD080910222222.txt
ER08EVENTCD080911232323.txt
ER08EVENTDC080911232324.txt

Which leave two choice. Cut out the 9th field from the data stream and then sort...
Code:
> cut -d" " -f9 file9a | sort -k1.10,23
ER08EVENTAA080910130150.txt
ER08EVENTAA080910130245.txt
ER08EVENTAA080910224550.txt
ER08EVENTAA080910232000.txt
ER08EVENTAA080910232010.txt
ER08EVENTAA080911015005.txt
ER08EVENTCD080910130245.txt
ER08EVENTCD080910222222.txt
ER08EVENTCD080911232323.txt
ER08EVENTDC080911232324.txt

Or, sort on the 9th field
Code:
> sort -k9.10,23 file9a
-rwx------ 1 dsadm dstage 81 Oct 03 11:38 ER08EVENTAA080910130150.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:06 ER08EVENTAA080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 10:53 ER08EVENTAA080910224550.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:51 ER08EVENTAA080910232000.txt
-rwx------ 1 dsadm dstage 81 Oct 03 10:56 ER08EVENTAA080910232010.txt
-rwx------ 1 dsadm dstage 81 Oct 03 11:47 ER08EVENTAA080911015005.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:22 ER08EVENTCD080910130245.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:23 ER08EVENTCD080910222222.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:34 ER08EVENTCD080911232323.txt
-rwx------ 1 dsadm dstage 81 Oct 03 12:35 ER08EVENTDC080911232324.txt

My previous example sorted on field 1, similar to the command you showed in your earlier example. (I assumed you cut to the 9th field so that you would sort on the field #1.)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find files and sort by timestamp

Used below command to get list of files sorted by timestamp find -L . -type f -name '*dat*' | xargs ls -ltrg I want to get only the filenames so I tried adding basename but it doenst work , can some one advise on how to get only file name (1 Reply)
Discussion started by: lalitpct
1 Replies

2. Shell Programming and Scripting

Sort Unique Column with the most recent timestamp

Hello, I have a sample file with the below contents : Backup Oracle8_P112_PEGA_Archivedel Completed full 10/11/2015 03:50:06PM Backup Oracle8_G567_PEGA_Archivedel Completed full 10/11/2015 01:15:56PM Backup Oracle8_P112_PEGA_Archivedel Completed full ... (8 Replies)
Discussion started by: rahul2662
8 Replies

3. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

4. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

5. Programming

Sort the files on their name and then the timestamp using Perl

Hi All, I am new to Perl. I have a scenario to code. In a folder I have number of files and they will start with P01 or P02 or P03 and so on..I have to sort them on name first and then on time stamp. Ex. File name timestamp P01_file1.txt 1PM P02_file1.txt 1AM P01_file2.txt 2PM... (12 Replies)
Discussion started by: unankix
12 Replies

6. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

7. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

8. UNIX for Dummies Questions & Answers

Shell script which will sort all the files in a directory with the timestamp they were created

Team, Pls help writing a shell script which will sort all the files in a directory with the timestamp they were created. (like ls -lrt) (6 Replies)
Discussion started by: asappidi
6 Replies

9. Shell Programming and Scripting

sort for timestamp information-YYYY-MM-DD HH:MM:SS

Dear all, Please advice how do I sort a file based on timestamp information. I want to sort the second column in asc/desc order which has timestamp information in format YYYY-MM-DD HH:MM:SS Example File - Input.txt contains cat ss.txt 100|2009-03-30 11:38:43 141|2009-06-01 12:12:01... (1 Reply)
Discussion started by: sureshg_sampat
1 Replies
Login or Register to Ask a Question