The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help in sort ali560045 Shell Programming and Scripting 2 12-04-2007 04:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 03:56 AM
du -h | sort ? fongthai Shell Programming and Scripting 6 11-02-2006 05:59 PM
How do i sort? abhijeetkul Shell Programming and Scripting 1 12-22-2005 12:49 AM
2nd sort key whatisthis UNIX for Dummies Questions & Answers 1 10-20-2004 07:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-29-2007
Registered User
 

Join Date: Apr 2007
Posts: 7
sort help

Before
2007-04-29 01:02:02.440
2007-04-28 01:02:02.446
2007-04-29 01:02:15.113
2007-05-29 01:02:15.113
2007-04-28 09:09:04.939
2007-04-29 09:09:04.952
2007-05-29 09:09:05.886
2007-04-29 09:09:05.887
2007-04-28 09:00:49.066

After
2007-04-28 01:02:02.446
2007-04-28 09:00:49.066
2007-04-28 09:09:04.939
2007-04-29 01:02:15.113
2007-04-29 09:09:03.965
2007-04-29 09:09:04.952
2007-05-29 01:02:15.113
2007-05-29 09:09:05.886


How do i sort this in ascending order of year month date and for each date the timestamps should be in ascending order...
Reply With Quote
Forum Sponsor
  #2  
Old 04-29-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,295
Code:
tr -s ':' ' ' < filename | tr -s '-' ' ' | sort | \
awk '{ printf("%s-%s-%s %s:%s:%s\n", $1, $2, $3, $4, $5, $6) }'
output
Code:
2007-04-28 01:02:02.446
2007-04-28 09:00:49.066
2007-04-28 09:09:04.939
2007-04-29 01:02:02.440
2007-04-29 01:02:15.113
2007-04-29 09:09:04.952
2007-04-29 09:09:05.887
2007-05-29 01:02:15.113
2007-05-29 09:09:05.886
Reply With Quote
  #3  
Old 04-29-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 550
Hi.

The before and after files are different in length.

Because you have the columns so nicely lined up, you should be able to use sort with just a few keys:
Code:
#!/bin/sh

# @(#) s1       Demonstrate sort for date and time.

# sample:
# 2007-04-29 01:02:02.440
# Everything is in columns, so just sort on key 1 and then key 2.

sort -k1,2 -k2 data1 |
tee data2

if ! cmp data2 results.txt
then
        echo
        echo " Results from diff:"
        echo
        diff -y -W72 --suppress-common-lines data2 results.txt
else
        echo
        echo " Files are identical."
fi

exit 1
Producing:
Code:
% ./s1
2007-04-28 01:02:02.446
2007-04-28 09:00:49.066
2007-04-28 09:09:04.939
2007-04-29 01:02:02.440
2007-04-29 01:02:15.113
2007-04-29 09:09:04.952
2007-04-29 09:09:05.887
2007-05-29 01:02:15.113
2007-05-29 09:09:05.886

 Files are identical.
where the file results.txt is from jim's post, data1 is your input data, and data2 is the sort output ... cheers, drl
Reply With Quote
  #4  
Old 04-29-2007
Registered User
 

Join Date: Apr 2007
Posts: 7
thx

Before
qa,2007-04-29 01:02:02.440
df,2007-04-28 01:02:02.446
gf,2007-04-29 01:02:15.113
df,2007-05-29 01:02:15.113
jh,2007-04-28 09:09:04.939
lk,2007-04-29 09:09:04.952
uj,2007-05-29 09:09:05.886
ki,2007-04-29 09:09:05.887
gy,2007-04-28 09:00:49.066

After
df,2007-04-28 01:02:02.446
gy,2007-04-28 09:00:49.066
jh,2007-04-28 09:09:04.939
qa,2007-04-29 01:02:02.440
gf,2007-04-29 01:02:15.113
lk,2007-04-29 09:09:04.952
ki,2007-04-29 09:09:05.887
df,2007-05-29 01:02:15.113
uj,2007-05-29 09:09:05.886

If the data is in the above format how can we sort?
Reply With Quote
  #5  
Old 04-29-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 550
Hi, unics.

You've seen two solutions to a similar problem.

How would you go about analyzing this problem after consulting man pages?

What commands and options would you use?

Show us some of your attempts and work ... cheers, drl
Reply With Quote
  #6  
Old 04-29-2007
Registered User
 

Join Date: Apr 2007
Posts: 7
Hi

I have tried

sort -t"," -k2.1,2.4n -k2.6,2.7n -k2.9,2.10n -k2.12,2.13n -k2.15,2.16n -k2.18,2.19n -k2.21,2.23n

but somehow seems to work sometimes..dont know whats the prob..anything that needs to be corrected?
Reply With Quote
  #7  
Old 04-29-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 550
Hi.

OK, it looks like you are working with fields and parts of fields.

How is this file different from the first one? ... cheers, drl
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0