The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 11:34 AM
Building Full-Text Search Applications with Oracle Text iBot Oracle Updates (RSS) 0 04-06-2008 02:10 AM
Perl Sort on Text File eltinator Shell Programming and Scripting 6 08-07-2007 11:20 AM
Need a Help with sort a text file with some fields alexcol Shell Programming and Scripting 3 02-19-2007 12:13 AM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 11:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-08-2008
Registered User
 

Join Date: Nov 2007
Location: Regina, SK, Canada
Posts: 12
Sort Text

Hello,

I have a text file that I need to sort the lines by date

record=5,French 9,2008-09-02T08:55:00,2008-09-02T10:00:00,2
record=79,Entrepreneurship 30,2008-09-17T11:00:00,2008-09-17T12:00:00,2
record=6,Computer Science 20,2008-09-02T09:55:00,2008-09-02T10:50:00,1
record=7,Entrepreneurship 30,2008-09-02T11:00:00,2008-09-02T12:00:00,2


Essentially I need to sort these lines by the start date & time which is bolded here. Is it possible to sort these lines by the date highlighted?

Thank you for your help
Dallas
Reply With Quote
Forum Sponsor
  #2  
Old 09-08-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
Try this:

Code:
awk -F, '{t=$3;gsub("[-T:]*","",t);print t,$0}' inputfile | sort -n -k 1,1 | cut -d " " -f 2- > outputfile
Essentially it creates a temporary field containing the date stamp minus all punctuation, sorts it by that field numerically, and removes that field again.
Reply With Quote
  #3  
Old 09-08-2008
Registered User
 

Join Date: Nov 2007
Location: Regina, SK, Canada
Posts: 12
I have to be honest with you. I have absolutely no idea how this works but it works very well. Thank you so much for your help.

Thank you Annihilannic
Dallas
Reply With Quote
  #4  
Old 09-09-2008
Registered User
 

Join Date: Nov 2007
Location: Regina, SK, Canada
Posts: 12
Hello,

Based in this same text format is there a way to extract all lines between two dates.

record=5,French 9,2008-09-05T08:55:00,2008-09-02T10:00:00,2
record=79,Entrepreneurship 30,2008-09-04T11:00:00,2008-09-17T12:00:00,2
record=6,Computer Science 20,2008-09-03T09:55:00,2008-09-02T10:50:00,1
record=7,Entrepreneurship 30,2008-09-02T11:00:00,2008-09-02T12:00:00,2

Essentially can I pull say the all items between September 3 at midnight and september 4th at midnight.

Thank you
Dallas
Reply With Quote
  #5  
Old 09-09-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
Try this:

Code:
awk -F, '{t=$3;gsub("[-T:]*","",t); if (t >= 20080903000000 && t < 20080904000000) print t,$0}' inputfile | sort -n -k 1,1 | cut -d " " -f 2- > outputfile
I'll take the opportunity to explain how it works:

awk -F, - run awk, setting the default input field separator to a comma.

The next part between the single quotes is the awk script:
{t=$3; - assign the third field to variable "t"
gsub("[-T:]*","",t); - substitute any occurrences of dash, capital T or a colon in the variable "t" with nothing
if (t >= 20080903000000 && t< 20080904000000) print t,$0} - print t followed by the original input line only if the timestamp is in the specified range

inputfile - the input data

| sort -n -k 1,1 - sort numerically by the first field

| cut -d " " -f 2 - remove the temporary first field which we used for sorting

> outputfile - and store the output in "outputfile"
Reply With Quote
  #6  
Old 09-10-2008
Registered User
 

Join Date: Nov 2007
Location: Regina, SK, Canada
Posts: 12
Thanks again, I really appreciate your help. I have been working on a custom scheduling software for an in house project at my work and your help with processing this data has really sped up my progress. Thank you
Dallas
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0