LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2001, 12:10 AM   #1
timnphx
LQ Newbie
 
Registered: Mar 2001
Posts: 6

Rep: Reputation: 0
Question


I have a file which is a group of records with fields (variable-length) seperated by a ":" example,
(first_name:last_name:soc-sec#:title:Hire_date) where date in is the form of 03-12-1999 for example.
I try to use the command (like my book says) to sort the records according to the hire_date. I have to sort it by the fields. So I tried the commands:

sort -+: +4 filename or
sort -t:+4 filename

but neither of these work and I get errors.

Got some info and exmples on the sort options for my book seams to be lacking?
 
Old 04-06-2001, 08:22 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Unhappy

I don't think that sort will every work for this application. With the way that you have your dates formatted, sort will always sort by the months before it ever considers a year. Take the following list of data:
  • 03-04-1999
  • 04-15-2000
  • 01-15-2001

You run the values through sort and you get these results:
  • 01-15-2001
  • 03-04-1999
  • 04-15-2000
You can see that it is completly wrong. Sort will only look at the year if the month and days are the same.

The only way that sort will work is if you dates are in the form: YYYYMMDD. This way it will look at year, month then day. It would sort this correctly. You could always convert the dates to the above format and then sort.

Good Luck

Gary
 
Old 04-06-2001, 11:39 PM   #3
timnphx
LQ Newbie
 
Registered: Mar 2001
Posts: 6

Original Poster
Rep: Reputation: 0
Use of sort on dated field

Actually, I found the answer from a friend in class.
Where the record had fields seperated by a ":' and the date was in the form of 02-12-1999 for example, we used the syntax:

sort -t : -k 9.7,9.10 filename

this was sort by the 9th field (-k) starting at the 7th character thru the 9th field 10th character, where the delimiter (-t) was the colon :

But thanks anyway.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line to sort files satimis Programming 6 06-28-2005 02:50 AM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
Activating case sensitivity in `sort' command? wirawan0 Linux - General 4 06-26-2004 10:20 AM
sort pantera Programming 5 05-26-2004 07:36 PM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration