Sorting the data with date

Hi,
PFB the data:

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:

Perl_Random_120519
Perl_Random_120528
C_Random_130417
Java_Random_130518

can u please help me on this?

sort -t'_' -k3n file
1 Like

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

You could do something dirty like this:

awk -F_ '{ print $NF,$0 }' file | sort -n | awk '{ print $NF }'

Thanks a lot...

Hi.

Non-standard utility msort allows you to specify fields from the right as well as the left:

#!/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:

$ ./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)

1 Like