Sort content of text file based on date?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort content of text file based on date?
# 1  
Old 02-02-2011
Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this:

Quote:
16 jan 2008 22:18

Text 1 from forum post Text 1 from forum post Text 1 from forum post

--------------------------------------------------------

26 jul 2005 18:33

Text 2 from forum post Text 2 from forum post Text 2 from forum post

--------------------------------------------------------

26 jul 2006 18:46

Text 3 from forum post Text 3 from forum post Text 3 from forum post

--------------------------------------------------------

26 jul 2003 18:54

Text 4 from forum post Text 4 from forum post Text 4 from forum post

--------------------------------------------------------
Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be:

Quote:
26 jul 2003 18:54

Text 4 from forum post Text 4 from forum post Text 4 from forum post

--------------------------------------------------------

26 jul 2005 18:33

Text 2 from forum post Text 2 from forum post Text 2 from forum post

--------------------------------------------------------

26 jul 2006 18:46

Text 3 from forum post Text 3 from forum post Text 3 from forum post

--------------------------------------------------------

16 jan 2008 22:18

Text 1 from forum post Text 1 from forum post Text 1 from forum post

--------------------------------------------------------
# 2  
Old 02-02-2011
Are the dash lines (---) part of the real input or you put them only for clearness?
# 3  
Old 02-02-2011
They are part of the input.
# 4  
Old 02-02-2011
Code:
perl -ne'BEGIN {
  @m{
    qw[ jan feb mar apr may jun jul aug sep oct nov dec ]
    } = 0 .. 11;
  } 
  do {
    @d = split/[\s:]/;
    $d = join "", $d[2], $m{$d[1]}, $d[0], $d[3], $d[4];
    } if /^\d{2} \w{3} \d{4} \d{2}:/;
  do {
    push @r, [$d, $r]; 
    undef $r; 
    next;
    } if /^---/;      
  $r .= $_;     
  END {
    print map {
      $_->[1] . ("-") x 50 . $/ 
      } sort { 
        $a->[0] <=> $b->[0] 
        } @r;
   }' infile


Last edited by radoulov; 02-02-2011 at 11:17 AM..
This User Gave Thanks to radoulov For This Post:
# 5  
Old 02-02-2011
Thanks.

I do get an error when I execute the script though:

Quote:
Bareword found where operator expected at sortera.pl line 25, near "}' output"
(Might be a runaway multi-line '' string starting on line 4)
(Missing operator before output?)
syntax error at sortera.pl line 4, near "-ne"
Execution of sortera.pl aborted due to compilation errors.
I use the filename output.txt as infile.
# 6  
Old 02-02-2011
Could you post the exact command that you executed (just copy/paste from your terminal)?

This is what I run:

Code:
zsh-4.3.11[t]% cat infile 
16 jan 2008 22:18

Text 1 from forum post Text 1 from forum post Text 1 from forum post

--------------------------------------------------------

26 jul 2005 18:33

Text 2 from forum post Text 2 from forum post Text 2 from forum post

--------------------------------------------------------

26 jul 2006 18:46

Text 3 from forum post Text 3 from forum post Text 3 from forum post

--------------------------------------------------------

26 jul 2003 18:54

Text 4 from forum post Text 4 from forum post Text 4 from forum post

--------------------------------------------------------

zsh-4.3.11[t]% perl -ne'BEGIN {
  @m{
    qw[ jan feb mar apr may jun jul aug sep oct nov dec ]
} = 0 .. 11;
  }
  do {
    @d = split/[\s:]/;
    $d = join "", $d[2], $m{$d[1]}, $d[0], $d[3], $d[4];
    } if /^\d{2} \w{3} \d{4} \d{2}:/;
  do {
    push @r, [$d, $r];
undef $r;
next;
} if /^---/;
  $r .= $_;
  END {
    print map{
  $_->[1] . ("-") x 50 . $/
  } sort {
    $a->[0] <=> $b->[0]
} @r;
   }' infile

26 jul 2003 18:54

Text 4 from forum post Text 4 from forum post Text 4 from forum post

--------------------------------------------------

26 jul 2005 18:33

Text 2 from forum post Text 2 from forum post Text 2 from forum post

--------------------------------------------------

26 jul 2006 18:46

Text 3 from forum post Text 3 from forum post Text 3 from forum post

--------------------------------------------------
16 jan 2008 22:18

Text 1 from forum post Text 1 from forum post Text 1 from forum post

--------------------------------------------------


Last edited by radoulov; 02-02-2011 at 11:17 AM..
# 7  
Old 02-02-2011
I must have missed something, now I get it to run through it at least. But it doesn't seem to sort it as it should.

For example, I get this:

Quote:
10 dec 2003 00:15

Är det där samma tjej?!

--------------------------------------------------

10 dec 2003 00:16

Oj, jag har blivit pappa. Det kom mer oväntat än jag väntat mig!

--------------------------------------------------

9 okt 2007 20:04

Fortsätter på den här tråden med årets bästa citat från DN:

DN: - Jag såg att Björns skott träffade bra. Men jag sköt ett andra skott eftersom vi visste vad det var för en sorts björn vi hade att göra med, säger Lars Svanberg.
***

Ja jag vet, jag är lättroad.

--------------------------------------------------

9 okt 2007 22:39

A Kyckling: Jag också i så fall.
***

Skönt att man inte är ensam då.

--------------------------------------------------

10 okt 2003 00:16

Här har du kaktusen! För att leda dig på rätt väg; jag är jävligt lik den här snubben (Richie Hawtin).

Ändrat 2003-10-09 kl. 23:22.

--------------------------------------------------
Those two posts from 2007 should obviously not be there. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort a text file based on names in square brackets

Hi all, I have a text file similar to this: Text More text Etc Stuff That Is Needed Etc Etc This contains over 70 entries and each entry has several lines of text below the name in square brackets. (5 Replies)
Discussion started by: Scally
5 Replies

2. Shell Programming and Scripting

Sort html based on .jar, .war file names and still keep text within three groups.

Output from zipdiff GNU EAR comparison tool produces output in html divided into three sections "Added, Removed, Changed". I want the output to be sorted by jar or war file. <html> <body> <table> <tr> <td class="diffs" colspan="2">Added </td> </tr> <tr><td> <ul>... (5 Replies)
Discussion started by: kchinnam
5 Replies

3. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

4. UNIX for Dummies Questions & Answers

How to sort a content of a text file using a shell script?

I am new to shell scripting. I am interested how to know how to sort a content of a file using shell scripting. I've attached the 'Input file' and the 'expected output' to this thread. Details provided in the expected output file will provide details on how the sort needs to be done. ... (16 Replies)
Discussion started by: nkarthik_mnnit
16 Replies

5. Shell Programming and Scripting

Sort outlook dump of emails based on date

Greetings! I have an export of an outlook database in text format. The emails however did not export by date and are all scattered in a massive file. I am hoping somebody out there could help me with a command that could sort the file based on the date. Here is the format of the email... (2 Replies)
Discussion started by: tntelle
2 Replies

6. Shell Programming and Scripting

Sort a file content using one column

Hello All, I have a file which have content as below. 03/09/2014 10:35 AM 618 Admin\rick pqr_ klm2_pog12_20140309_c.xlsx 03/10/2014 10:35 AM 618 user\test01 mplz_ fgh2_lal12_20140310_c.xlsx 03/17/2014 10:35 AM 618 Admin\vick abc_ xyz2_bc12_20140317_c.xlsx 03/18/2014 ... (2 Replies)
Discussion started by: kumar30213
2 Replies

7. Shell Programming and Scripting

Sort a file content with space

Hello, I need help on. I have a File which stores the information as below. It is space separated file, I want to keep only unique record in file based on file name. Also if you notice sometime filename with space appear in last column like (abc_ xyz1_bc12_20140312_c.xlsx) 03/17/2014 ... (9 Replies)
Discussion started by: kumar30213
9 Replies

8. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

9. Shell Programming and Scripting

Help with allocated text content based on specific rules...

Input file format: /tag="ABL" /note="abl homolog 2 /tag="ABLIM1" /note="actin binding LIM 1 /tag="ABP1" /note="amiloride binding protein 1 (amine oxidase (copper- containing)) /tag="ABR" /note="active BCR-related /tag="AC003042.1" /note="SDR family member 11 precursor . . . (4 Replies)
Discussion started by: perl_beginner
4 Replies

10. Shell Programming and Scripting

sort file text by date as a "ls -t"

I have, a file wich list file on server ftp and i want to sort the file text as the command "ls -t" i.e drwxr-x--- 2 ftp_mont System 0 Sep 30 09:16 . drwxr-x--- 2 ftp_mont System 0 Jul 15 2009 .. -rwxr-x--- 1 ftp_mont System 343998791 Sep 01 09:20 manif... (14 Replies)
Discussion started by: protocomm
14 Replies
Login or Register to Ask a Question