# Sort on one column only

**URL:** <https://community.unix.com/t/sort-on-one-column-only/293423>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 12, 2011, 3:28pm UTC](https://community.unix.com/t/sort-on-one-column-only/293423 "2011-08-12T15:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gio001](https://community.unix.com/letter_avatar/gio001/32/6_22734026db12803ebb3e059622ac3534.png) [@gio001](https://community.unix.com/u/gio001)\
**Post date:** [August 12, 2011, 3:28pm UTC](https://community.unix.com/t/sort-on-one-column-only/293423/1 "2011-08-12T15:28:32Z")

</div>

Hello,  
I am running on AIX.I have a question about sorting in UNIX.  
if my file is something like this:

a c  
b d  
a b  
b c  
a a

I want to sort on column 1 only. The following statement does not seem to work, it still considers the rest of the line in the sorting results:

```nohighlight
sort -k 1,1 inputfile

```

Any idea on what I could do to achieve a sorted result like this :

a c  
a b  
a a  
b d  
b c

Thanks

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [August 12, 2011, 3:56pm UTC](https://community.unix.com/t/sort-on-one-column-only/293423/2 "2011-08-12T15:56:55Z")

</div>

```nohighlight
$ cat -n sample5.txt | sort -k 2,2 | awk '{print $2,$3}'
a c
a b
a a
b d
b c

```

The -n with teh cat puts line numbers in, and that seemed to solve your issue.  
I then used awk to get rid of that first field.

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [August 12, 2011, 5:13pm UTC](https://community.unix.com/t/sort-on-one-column-only/293423/3 "2011-08-12T17:13:25Z")

</div>

sort will always look at the entire line once all the keys it's been given have compared equal.

That said, your desired sort result looks like sorting based on the first field followed by reverse sort on the second field. If that's not just a coincidence, this gives me the result you seek:

```nohighlight
sort -k1,1 -k2,2r file

```

Regards,  
Alister
