sort with different delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort with different delimiters
# 1  
Old 12-05-2011
sort with different delimiters

I have a file with the following lines in it:
Code:
Inbound1:remote - - 01/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound1:remote - - 02/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound3:remote - - 01/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound4:remote - - 02/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound5:remote - - 01/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound6:remote - - 01/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:08:20:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound9:remote - - 02/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP

I would like to sort the file by the date and time oldest first:
(Example of how I would like to see the output)
Code:
Inbound5:remote - - 01/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:08:20:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound3:remote - - 01/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound1:remote - - 01/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound6:remote - - 01/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound4:remote - - 02/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound9:remote - - 02/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound1:remote - - 02/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP

The different delimiters (space, slash, and colon) has me confused on how I could use the sort (AIX ksh) command. Anyone have any suggestions on how I can sort it?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-05-2011 at 03:41 PM.. Reason: code tags, please!
# 2  
Old 12-05-2011
Code:
awk '{ d = $4; gsub(/[/]/, ":", d); print d, $0 }' infile |
    sort -t: -k3,3n -k2,2M -k1,1n -k4n |
      cut -d\  -f2-

I'm not sure if the expected output you've posted is correct ...

Code:
01/Nov/2011:08:29:51
...
02/Oct/2011:08:29:52

?
# 3  
Old 12-05-2011
See if this works for you:
Code:
sort -k4.8,4.11 -k4.4,4.6M -k4.1,4.2 -k4.13,4.14 -k4.16,4.17 -k4.19,4.20 Inp_File

# 4  
Old 12-05-2011
Good point Shell_Life!
For fixed data I would recommend a single sort command too.
# 5  
Old 12-05-2011
Thanks for the reply. I tried but it seems AIX does not have a month option for sort. Do you have any idea for the month?

Code:
$ sort -k4.8,4.11 -k4.4,4.6M -k4.1,4.2 -k4.13,4.14 -k4.16,4.17 -k4.19,4.20 myfile
Usage:   sort   [-Abcdfimnru] [-T Directory] [-t Character] [-o File]
                [-y[Kilobytes]] [-z Recordsize] [-k Keydefinition]...
                [[+Position1][-Position2]]... [File]...
$ sort -k4.8,4.11 -k4.4,4.6m -k4.1,4.2 -k4.13,4.14 -k4.16,4.17 -k4.19,4.20 myfile
Usage:   sort   [-Abcdfimnru] [-T Directory] [-t Character] [-o File]
                [-y[Kilobytes]] [-z Recordsize] [-k Keydefinition]...
                [[+Position1][-Position2]]... [File]...
$ sort -k4.8,4.12 -k4.1,4.2 -k4.12,4.20 myfile
Inbound5:remote - - 01/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound5:remote - - 10/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound5:remote - - 11/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound5:remote - - 12/Dec/2010:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:08:20:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:08:20:22 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:08:21:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound1:remote - - 01/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound3:remote - - 01/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound4:remote - - 02/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound9:remote - - 02/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound1:remote - - 02/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound6:remote - - 01/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:09:20:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:09:20:22 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 02/Oct/2011:09:20:22 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 02/Oct/2011:09:20:23 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 02/Oct/2011:09:20:24 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 02/Oct/2011:09:20:25 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 03/Oct/2011:09:20:22 -0500 "GET / HTTP/1.1" 404 2098 HTTP
Inbound7:remote - - 01/Oct/2011:09:21:21 -0500 "GET / HTTP/1.1" 404 2098 HTTP

# 6  
Old 12-06-2011
Code:
perl -ane'BEGIN {
  %m2n = qw(
    Jan 1  Feb 2  Mar 3  Apr 4  May 5  Jun 6
    Jul 7  Aug 8  Sep 9  Oct 10 Nov 11 Dec 12
    );
  }
  @dt = split /[\/:]/, $F[3];
  push @r, [(join //, $dt[2], $m2n{$dt[1]}, $dt[0], @dt[3..5]), $_];
  END {  
  print map $_->[1],  
    sort { $a->[0] <=> $b->[0] }
      @r;
    }' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delimiters with awk?

I have a file which is separated by delimiter "|", but the prob is one of my column do contain delimiter as description so how can i differentiate it? PS : the delmiter does have backslash coming before it, if occurring in column Annual|Beleagured|Desc|Denver... (2 Replies)
Discussion started by: nikhil jain
2 Replies

2. Shell Programming and Scripting

Inserting Delimiters

Hi Team, I am trying to get the data in below format Jan 01 | 19:00:32 | xyz | abc | sometext | string however I am not sure of the total number strings which can come in the record hence i cant use something like below as it can end $6 or it can go further cat file| awk... (8 Replies)
Discussion started by: rakesh_411
8 Replies

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

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. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

6. Shell Programming and Scripting

Split using two delimiters

I'm trying to do a split using two delimiters. The first delimiter is ": " (or we could call it :\s). The second is "\n". How can or these delimiters so I can toss the values into an array without issue? I tried @array = split /:\s|\n/, $myvar; This doesn't seem to be working. Any an... (3 Replies)
Discussion started by: mrwatkin
3 Replies

7. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

8. Shell Programming and Scripting

Problems with delimiters

Hello, I have data in a file something like this - UNB+UNOA:1+006415160:1+AR0000012360:ZZ+080701:0552+2++DELFOR++++T'UNH+2+DELFOR:D:97A:UN Here, the delimiters used are + , : and ' . I have a set of such files in which these delimiters vary from one file to another. I am developing a... (4 Replies)
Discussion started by: The Observer
4 Replies

9. Solaris

To extract everything between two delimiters

My input file looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (1 Reply)
Discussion started by: dowsed4u8
1 Replies

10. Shell Programming and Scripting

Delimiters missing

Hi I have a pipe-delimited file where I eventually need to replace a string stored on the 3th field on a specific record. This is how the file looks like: A|Mike|Lvl 1|... B|... A|Maria|Lvl 1|... C|... B|... A|Jimmy|Lvl 2|... C|... A|Carry|Lvl 0|... C|... B|... A|John|Lvl 8|...... (2 Replies)
Discussion started by: Indalecio
2 Replies
Login or Register to Ask a Question