|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
linux sort command
This is the question being asked: (Sort your data file by last name first, then by the first name second – save as first_last.) I am not quite sure of the type of sort I am being asked to perform. I have read the man pages of the sort command a few times, as well as searching online for possible solutions and I have found neither. Here's an example file with which I have come up with to try and solve the above:
sort -nrk3 phone_list.txt > first_last Mark Jacobs 01 Mark Davis 03 Kerry Wild 05 Parker Jacobs 08 Valencia Community College. Orlando, FL USA. Prof. D. Weeks. COP-2341-12143. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
I think it can't be achieved with single sort run. This works, but I don't know if it is not too complicated for your course. Anyway here it is: Code:
awk '{print $2}' filename | sort | uniq | while read last_name; do awk -vn=$last_name '$2==n' filename | sort -k1; done |
| The Following User Says Thank You to bartus11 For This Useful Post: | ||
demet8 (10-01-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I tried your script but when I pipe file 1 into file 2 I get fatal cannot open file 2 for reading(no such file or directory). I created file 2 & tried it again after the error to see if it would work but it still failed to pipe into file 2. I initially thought awk was the solution also but this is an introductory Linux course so I felt the same way you did about the complexity of the question. Thanks for your help. I'll keep trying and hopefully I'll get it soon.
|
|
#4
|
||||
|
||||
|
To pipe the output to another file: Code:
awk '{print $2}' filename | sort | uniq | while read last_name; do awk -vn=$last_name '$2==n' filename | sort -k1; done > first_last |
| The Following User Says Thank You to bartus11 For This Useful Post: | ||
demet8 (10-19-2012) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
I must be missing something very basic here. Assuming that the first field in your input file is the first name and the second field in your input file is the last name, why isn't: Code:
sort -k2 phone_list.txt > first_last sufficient. |
| The Following User Says Thank You to Don Cragun For This Useful Post: | ||
demet8 (10-19-2012) | ||
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Try running that code on a bit modified input: Code:
Mark Jacobs 01 Mark Davis 03 Kerry Wild 05 Parker Jacobs 08 Adam Jacobs 08 Notice where "Adam" is placed. |
| The Following 2 Users Say Thank You to bartus11 For This Useful Post: | ||
demet8 (10-01-2012), Don Cragun (10-01-2012) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
You're right. I'm not doing well today... Once more:
Quote:
|
| The Following User Says Thank You to Don Cragun For This Useful Post: | ||
demet8 (10-01-2012) | ||
| Sponsored Links | ||
|
![]() |
| Tags |
| sort by column, sort field |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| LINUX SORT command chops results | Yahalom | UNIX for Dummies Questions & Answers | 4 | 04-04-2012 09:44 AM |
| sort command in centos linux os | jpachar | Linux | 1 | 02-22-2012 04:52 AM |
| linux sort command produces strange output | ajb | UNIX for Dummies Questions & Answers | 3 | 08-11-2011 12:17 PM |
| How to Sort Floating Numbers Using the Sort Command? | daniel.gbaena | Shell Programming and Scripting | 7 | 05-16-2010 11:20 AM |
| Linux Sort command | itisijayare | UNIX for Dummies Questions & Answers | 1 | 04-03-2009 02:05 PM |
|
|