The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 07:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 06:56 AM
du -h | sort ? fongthai Shell Programming and Scripting 6 11-02-2006 08:59 PM
How do i sort? abhijeetkul Shell Programming and Scripting 1 12-22-2005 03:49 AM
2nd sort key whatisthis UNIX for Dummies Questions & Answers 1 10-20-2004 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-29-2007
unics unics is offline
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...
  #2 (permalink)  
Old 04-29-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
  #3 (permalink)  
Old 04-29-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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
  #4 (permalink)  
Old 04-29-2007
unics unics is offline
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?
  #5 (permalink)  
Old 04-29-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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
  #6 (permalink)  
Old 04-29-2007
unics unics is offline
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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0