Sorting the data with date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting the data with date
# 1  
Old 06-10-2013
Sorting the data with date

Hi,
PFB the data:
Code:
C_Random_130417
Java_Random_130518
Perl_Random_120519
Perl_Random_120528

so the values are ending with year,i.e.,130417[yymmdd]
i want to sort the values with date.
i want the output like this:
Code:
Perl_Random_120519
Perl_Random_120528
C_Random_130417
Java_Random_130518

can u please help me on this?

Last edited by Franklin52; 06-11-2013 at 03:08 AM.. Reason: Please use code tags
# 2  
Old 06-10-2013
Code:
sort -t'_' -k3n file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-10-2013
Thanks....
It works fine...
But if the values are not same type
C_Random_Random1_130724
Java_Random_130523
Then what will be the command
# 4  
Old 06-10-2013
You could do something dirty like this:
Code:
awk -F_ '{ print $NF,$0 }' file | sort -n | awk '{ print $NF }'

# 5  
Old 06-10-2013
Thanks a lot...
# 6  
Old 06-11-2013
Hi.

Non-standard utility msort allows you to specify fields from the right as well as the left:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate specify field from right end, msort.
# See: http://freecode.com/projects/msort

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
head $FILE

pl " Results:"
msort -q -l -j -d "_" --position -1 -c n $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
C_Random_Random1_130724
Java_Random_130523
C_Random_130417
Java_Random_130518
Perl_Random_120519
Perl_Random_120528

-----
 Results:
Perl_Random_120519
Perl_Random_120528
C_Random_130417
Java_Random_130518
Java_Random_130523
C_Random_Random1_130724

The msort utility is available in many repositories, distributions of Linux, FreeBSD (ports), etc.

See the URL in the comment in the script for more ways to get msort.

Best wishes ... cheers, drl (125)
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting on fields for last date

Hi all, I have a file with a list of rpm's that have different dates. I am trying to just grab the latest rpm and install date, and discard the rest. The file has 1000's of entries all with different names and dates. I have tried sort -k on the file and I am not grabbing the info, ... (4 Replies)
Discussion started by: gartie
4 Replies

2. Shell Programming and Scripting

Plz Help in sorting the data on date basis

I have file a.txt having below data cat a.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493... (5 Replies)
Discussion started by: ranabhavish
5 Replies

3. UNIX for Dummies Questions & Answers

Plz Help in sorting the data on date basis

I have file having below data 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493 01-JUL-13... (2 Replies)
Discussion started by: ranabhavish
2 Replies

4. Shell Programming and Scripting

Sorting data file by date and time

Hello. Sorting data file by date and time with the following issues: Date is in the following format m/d/yyyy, no leading zeros Time is in the following format h:m:s AM/PM, no leading zeros Any ideas on how to sort data when the above issues? Could the date/time be converted inline to... (5 Replies)
Discussion started by: JimBurns
5 Replies

5. Shell Programming and Scripting

Sorting by date

I am trying to sort by two columns. The first column in an ID, the second is a date in the form yyyy-mm-dd. I need to sort by the ID column, then in ascending order for the date column (earliest date to most recent date compared to today). Input data: 012-abc 2012-04-25 ... (3 Replies)
Discussion started by: mollydog11
3 Replies

6. UNIX and Linux Applications

Matlab (more generally: sorting data by date)

I'm not sure if this really belongs anywhere on this forum but my previous experiences on here have shown me that you guys are very helpful so I figure I may as well try. I have a bunch of large 2d arrays in matlab and each has a column for a date that each row corresponds to. The format is... (1 Reply)
Discussion started by: skray
1 Replies

7. UNIX for Dummies Questions & Answers

Sorting by date and time

Hi guys... I've been trying to do this for ages. Maybe you can help. I have log files like the examples below and I have grepped out certain lines from the files so that I can get an idea of who is logging on and how. So now I have the information in a new file but it is now in a different order... (7 Replies)
Discussion started by: padmundo
7 Replies

8. Shell Programming and Scripting

Date Sorting

Hi, I have a list of files that take on the format ABCDE_yymmdd and wish to sort them in ascending date order. I can't use the unix time stamp for the file as this could possibly be different from the date given in the file name. Does anyone know of any way this can be done using unix shell... (14 Replies)
Discussion started by: LiquidChild
14 Replies

9. UNIX for Dummies Questions & Answers

date sorting

Hi at all, I have to sort a log file on timestamp field. That's field is the third! a log file sample..... 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:23|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:16|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:20|0.3.8... (3 Replies)
Discussion started by: nmilella
3 Replies

10. UNIX for Dummies Questions & Answers

sorting on date

I have a file where dates in the form mm/dd/yyyy is the first field. How do I sort the file by the date field? Thanks, Duckman (6 Replies)
Discussion started by: Duckman
6 Replies
Login or Register to Ask a Question