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.

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:

awk '{print $2}' filename | sort | uniq | while read last_name; do awk -vn=$last_name '$2==n' filename | sort -k1; done
1 Like

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.

To pipe the output to another file:

awk '{print $2}' filename | sort | uniq | while read last_name; do awk -vn=$last_name '$2==n' filename | sort -k1; done > first_last
1 Like

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:

sort -k2 phone_list.txt > first_last

sufficient.

1 Like

Try running that code on a bit modified input:

Mark Jacobs 01
Mark Davis 03
Kerry Wild 05
Parker Jacobs 08
Adam Jacobs 08

Notice where "Adam" is placed.

2 Likes

You're right. I'm not doing well today... Once more:

1 Like

Hey Don I think your right. I used that solution first but I felt it was too trivial initially. Now thinking about it again, I definitely think I read into the question a bit much. Thank you for your help. @bartus11 I will definitely try your script again and or use it for further reference. Thanks a lot.

There are some "sort" intrinsics to be considered and i think you haven't covered them all yet. This might not have any impact on your test file, but may well change the sorting order in another sample.

Lets see:

The default behavior of "sort" is to sort from the field/position given in te argument to the "-k" option to the end of line. That means:

sort -k 2 /some/file

will sort on field 2 first, in case f2 is equal on field 3, if this is equal too on field 4, etc. to the end of line. As you want to search on a last name - first name basis you have to state that:

sort -k 2,2 -k 1,1

You are lucky that you use only full fields, because it is possible to base sorting order on a sub-field starting at the n-th character of a certain field. Alas, character-numbering is sometimes 1-based and sometimes zero-based, depending on "-t" or "-b" being used - as i learned myself recently the hard way.

I hope this helps.

bakunin

Very close... According to the standards (and the sort utilities I've used tend to follow the standards pretty closely), what you said about sort -k 2 is exactly correct. But, sort -k 2,2 sorts on the 2nd field and if two or more lines compare equal on that field, the entire line is used as the secondary sort key. So sort -k 2,2 performs exactly the same sort as sort -k 2,2 -k 1,1 .

Thanks everyone. I appreciate your help.