Sponsored Content
Full Discussion: Sorting a date coulumn
Top Forums Shell Programming and Scripting Sorting a date coulumn Post 302117586 by vgersh99 on Tuesday 15th of May 2007 12:29:54 PM
Old 05-15-2007
nawk -f rinku.awk abc.txt | sort -n | cut -d' ' -f2-

rinku.awk
Code:
BEGIN {
      monthA="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
      monN=split(monthA, monA, FS)
      # invert a monA array to be indexed by the NAME of the month
      for(i=1; i<=monN; i++) {
        monA[monA[i]]=i
        delete monA[i]
      }
}
{
   _day=substr($3,1,2)
   _mon=substr($3,3,3)
   _year=substr($3,6)
   printf("%4d%02d%02d%s%s\n", _year, monA[_mon], _day, OFS, $0)
}

Or all in ksh:
Code:
#!/bin/ksh

monthList='JanFebMarAprMayJunJulAugSepOctNovDec'

month2number() # $1 = month
{
    typeset monthName="${1}"
    typeset idx;
    idx=${monthList%%${monthName}*}
    printf "%02d" "$(( (${#idx} + 3 ) / 3 ))"
}

while read one two date four junk
do
    _day=$(echo ${date} | sed 's/^\(..\).*/\1/')
    _mon=$(echo ${date} | sed 's/^..\(...\).*/\1/')
    _year=$(echo ${date} | sed 's/.*\(....\)/\1/')
    echo "${_year}$(month2number ${_mon})${_day} " " $one " " $two " " $date " " $four"
done < abc.txt | sort -n | cut -d' ' -f2-

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Date and Time sorting

Hi Guys! i have a problem of sorting column chronologically because the data i have in column is in the following format 06/Dec/2006:18:09:54 and need to be sorted in the following way (upto seconds) 06/Dec/2005:18:09:50 06/Dec/2005:18:09:51 31/Mar/2006:19:30:41 24/Oct/2006:19:16:19... (4 Replies)
Discussion started by: me_newbie
4 Replies

6. Shell Programming and Scripting

date(ddmmyyyy) sorting

input : 20110730 20110730 20110731 20110731 20110801 20110801 20110801 20110813 20110815 01062011 01062011 OUTPUT : i need to sort this input in such a way so that the latest date comes first. (11 Replies)
Discussion started by: urfrnddpk
11 Replies

7. Shell Programming and Scripting

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 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... (5 Replies)
Discussion started by: arindam guha
5 Replies

8. 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

9. Shell Programming and Scripting

Sorting on date basis

I have file data.txt having below data cat data.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 FC 1514 0000764631 01-NOV-13 2.38.11.00.100089 FC 1514 0000856571 01-NOV-13 2.38.11.00.100089 IN 300.32... (1 Reply)
Discussion started by: ranabhavish
1 Replies

10. 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
Perl6::Say(3pm) 					User Contributed Perl Documentation					   Perl6::Say(3pm)

NAME
Perl6::Say - "print" -- but no newline needed SYNOPSIS
# Perl 5 code... use Perl6::Say; say 'boo'; # same as: print 'boo', " " say STDERR 'boo'; # same as: print STDERR 'boo', " " STDERR->say('boo'); # same as: print STDERR 'boo', " $fh->say('boo'); # same as: print $fh 'boo', " "; say(); # same as: print "$_ "; say undef; # same as: print " "; DESCRIPTION
Note for Users of Perl 5.10 You don't need this module. The Perl 6 "say" function is available in Perl 5.10 by saying "use feature 'say';". Hence, this module is of interest only to users of Perl 5.6 and 5.8. If you have Perl 5.10 installed, see the 510/ directory in this distribution for some elementary examples of "say" taken from "perldoc fea- ture". General Implements a close simulation of the "say" function in Perl 6, which acts like "print" but automatically appends a newline. Use it just like "print" (except that it only supports the indirect object syntax when the stream is a bareword). That is, assuming the relevant filehandles are open for output, you can use any of these: say @data; say FH @data; FH->say(@data); *FH->say(@data); (*FH)->say(@data); say $fh, @data; $fh->say(@data); but not any of these: say {FH} @data; say {*FH} @data; say {*FH} @data; say $fh @data; say {$fh} @data; Additional Permitted Usages As demonstrated in the test suite accompanying this distribution, "Perl6::Say::say()" can be used in all the following situations. $string = q{}; open FH, ">", $string; say FH qq{Hello World}; # print to a string close FH; # requires Perl 5.8.0 or later use FileHandle; $fh = FileHandle->new($file, 'w'); if (defined $fh) { say $fh, qq{Hello World}; $fh->close; } use IO::File; $fh = IO::File->new($file, 'w'); if (defined $fh) { say $fh, qq{Hello World}; $fh->close; } $string = q{}; open FH, ">", $string; # requires Perl 5.8.0 or later select(FH); say qq{Hello World}; close FH; Interaction with Output Record Separator In Perl 6, "say @stuff" is exactly equivalent to "Core::print @stuff, " "". That means that a call to "say" appends any output record separator (ORS) after the added newline (though in Perl 6, the ORS is an attribute of the filehandle being used, rather than a global $/ variable). "IO::Handle::say()" IO::Handle version 1.27 or later (which, confusingly, is found in IO distribution 1.23 and later) also implements a "say" method. Perl6::Say provides its own "say" method to IO::Handle if "IO::Handle::say" is not available. Usage with Older Perls As noted above, some aspects of "Perl6::Say::say()" will not work with versions of Perl earlier than 5.8.0. This is not due to any problem with this module; it is simply that Perl did not support printing to an in-memory file ("print $string, " ";") prior to that point. (Thanks to a CPAN testers report from David Cantrell for identifying this limitation.) WARNING
The syntax and semantics of Perl 6 is still being finalized and consequently is at any time subject to change. That means the same caveat applies to this module. DEPENDENCIES
No dependencies other than on modules included with the Perl core as of version 5.8.0. Some of the files in the test suite accompanying this distribution use non-core CPAN module IO::Capture::Stdout. Tests calling IO::Cap- ture::Stdout methods are enclosed in "SKIP" blocks and so should pose no obstacle to installation of the distribution on systems lacking IO::Capture. (However, the maintainer strongly recommends IO::Capture for developers who write a lot of test code. So please consider installing it!) AUTHOR and MAINTAINER AUTHOR Damian Conway (damian@conway.org). MAINTAINER James E Keenan (jkeenan@cpan.org) (effective v0.06, July 2006). ACKNOWLEDGMENTS
Thanks to Damian Conway for dreaming this up. Thanks to David A Golden for a close review of the documentation. Thanks to CPAN tester Jost Krieger for reporting an error in my SKIP block count in one test file. BUGS AND IRRITATIONS
As far as we can determine, Perl 5 doesn't allow us to create a subroutine that truly acts like "print". That is, one that can simultane- ously be used like so: say @data; and like so: say {$fh} @data; Comments, suggestions, and patches welcome. COPYRIGHT
Copyright (c) 2004, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.8.8 2008-02-09 Perl6::Say(3pm)
All times are GMT -4. The time now is 07:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy