![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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... |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
tr -s ':' ' ' < filename | tr -s '-' ' ' | sort | \
awk '{ printf("%s-%s-%s %s:%s:%s\n", $1, $2, $3, $4, $5, $6) }'
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
|
||||
|
||||
|
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
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. |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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? |
|
#7
|
||||
|
||||
|
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 |
||||
| Google The UNIX and Linux Forums |