Results 1 to 4 of 4
Hello,
This is going to sound like homework, because it is. I have been trying for a couple hours and cant figure it out, I have read every faq on ...
- 03-10-2009 #1Just Joined!
- Join Date
- May 2006
- Posts
- 2
Reordering Field durring Sort
Hello,
This is going to sound like homework, because it is. I have been trying for a couple hours and cant figure it out, I have read every faq on sort.
I need to take a file formated as the following
first:last:city:state
sort it by last name
Display only the ones from a certain state and display it in this order
last:first:city:state
Normally I wouldn't post homework, but this is a project, and I am STUMPED! I have hit a brick wall in my script.
Any help is greatly appreciated.
- 03-10-2009 #2Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Hints:
awk [ study it @ Awk - A Tutorial and Introduction - by Bruce Barnett ]
sort (since you have read the faqs about sort I believe you have ideas how to sort by specific column )
- 03-11-2009 #3
Okay, to me this is really fun (I love Unix text processing). Here is a fairly easily-grasped way to do it, but it uses multiple programs. Sadly I don't have access to a "real" Operating System at this moment, so I am doing this by memory.
This uses sed, the stream editor. You tell it not to print with -n, then you pass it its arguments, namely to print (p) lines containing $STATE. Obviously you would replace $STATE with the state you need to filter out.Code:sed -n "/$STATE/p
As for the sorting by last name, you use sort for this (note: this might not work, as my knowledge is fuzzy here)
I believe that this tells sort to use : as a delimiter, and to sort by the second column.Code:sort -t ":" +1
Thus you would just pipe these two commands together and send it to some output.
- 03-12-2009 #4
I'm sorry, I think the correct syntax is
sort -t : -k 2
To sort by the second column using : as the delimiter.


Reply With Quote