# linux sort command

**URL:** <https://community.unix.com/t/linux-sort-command/317549>\
**Category:** Homework & Coursework Questions\
**Created:** [October 1, 2012, 5:32pm UTC](https://community.unix.com/t/linux-sort-command/317549 "2012-10-01T17:32:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![demet8](https://community.unix.com/letter_avatar/demet8/32/6_449c533f586939b6e5f246632a46421f.png) [@demet8](https://community.unix.com/u/demet8)\
**Post date:** [October 1, 2012, 5:32pm UTC](https://community.unix.com/t/linux-sort-command/317549/1 "2012-10-01T17:32:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [October 1, 2012, 5:48pm UTC](https://community.unix.com/t/linux-sort-command/317549/2 "2012-10-01T17:48:20Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![demet8](https://community.unix.com/letter_avatar/demet8/32/6_449c533f586939b6e5f246632a46421f.png) [@demet8](https://community.unix.com/u/demet8)\
**Post date:** [October 1, 2012, 6:09pm UTC](https://community.unix.com/t/linux-sort-command/317549/3 "2012-10-01T18:09:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [October 1, 2012, 6:15pm UTC](https://community.unix.com/t/linux-sort-command/317549/4 "2012-10-01T18:15:45Z")

</div>

To pipe the output to another file:

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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [October 1, 2012, 6:16pm UTC](https://community.unix.com/t/linux-sort-command/317549/5 "2012-10-01T18:16:38Z")

</div>

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:

```nohighlight
sort -k2 phone_list.txt > first_last

```

sufficient.

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [October 1, 2012, 6:18pm UTC](https://community.unix.com/t/linux-sort-command/317549/6 "2012-10-01T18:18:45Z")

</div>

Try running that code on a bit modified input:

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

```

Notice where "Adam" is placed.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [October 1, 2012, 6:45pm UTC](https://community.unix.com/t/linux-sort-command/317549/7 "2012-10-01T18:45:10Z")

</div>

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

> [@](#):
>
> sort -k2,2 phone\_list.txt \> first\_last

---

<div class="post-metadata">

**Author:** ![demet8](https://community.unix.com/letter_avatar/demet8/32/6_449c533f586939b6e5f246632a46421f.png) [@demet8](https://community.unix.com/u/demet8)\
**Post date:** [October 1, 2012, 6:48pm UTC](https://community.unix.com/t/linux-sort-command/317549/8 "2012-10-01T18:48:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [October 2, 2012, 5:28am UTC](https://community.unix.com/t/linux-sort-command/317549/9 "2012-10-02T05:28:42Z")

</div>

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:

```plaintext
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:

```plaintext
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](http://www.unix.com/shell-programming-scripting/198607-solved-sort-numeric-part-field.html) the hard way.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [October 2, 2012, 8:43am UTC](https://community.unix.com/t/linux-sort-command/317549/10 "2012-10-02T08:43:39Z")

</div>

> [@bakunin](#):
>
> 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:
> 
> ```plaintext
> 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:
> 
> ```plaintext
> 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](http://www.unix.com/shell-programming-scripting/198607-solved-sort-numeric-part-field.html) 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` .

---

<div class="post-metadata">

**Author:** ![demet8](https://community.unix.com/letter_avatar/demet8/32/6_449c533f586939b6e5f246632a46421f.png) [@demet8](https://community.unix.com/u/demet8)\
**Post date:** [October 19, 2012, 11:59pm UTC](https://community.unix.com/t/linux-sort-command/317549/11 "2012-10-19T23:59:57Z")

</div>

Thanks everyone. I appreciate your help.
