linux sort command | Unix Linux Forums | Homework & Coursework Questions

  Go Back    


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.

linux sort command

Homework & Coursework Questions


Tags
sort by column, sort field

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-01-2012
demet8 demet8 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 12 November 2012, 5:15 PM EST
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
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  
Old 10-01-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 July 2014, 3:47 PM EDT
Posts: 3,707
Thanks: 7
Thanked 1,141 Times in 1,112 Posts
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  
Old 10-01-2012
demet8 demet8 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 12 November 2012, 5:15 PM EST
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
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  
Old 10-01-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 July 2014, 3:47 PM EDT
Posts: 3,707
Thanks: 7
Thanked 1,141 Times in 1,112 Posts
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  
Old 10-01-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 July 2014, 4:32 PM EDT
Location: San Jose, CA, USA
Posts: 4,211
Thanks: 165
Thanked 1,434 Times in 1,215 Posts
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  
Old 10-01-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 July 2014, 3:47 PM EDT
Posts: 3,707
Thanks: 7
Thanked 1,141 Times in 1,112 Posts
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  
Old 10-01-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 July 2014, 4:32 PM EDT
Location: San Jose, CA, USA
Posts: 4,211
Thanks: 165
Thanked 1,434 Times in 1,215 Posts
You're right. I'm not doing well today... Once more:
Quote:
sort -k2,2 phone_list.txt > first_last
The Following User Says Thank You to Don Cragun For This Useful Post:
demet8 (10-01-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 05:39 PM.