6

For Example suppose I have the following in a text file myfile.txt

Schwifty1 Schwifty2 Schwifty3 Schwifty4 Schwifty5 Schwifty6 Schwifty7 Schwifty8 Schwifty9 Schwifty10 ... ect ect ect... Schwifty20 Schwifty21 ... ect ect ect... Schwifty30 Schwifty31 

using sort on this file will return something like:

Schwifty1 Schwifty10 Schwifty11 ......... Schwifty2 Schwifty20 Schwifty21 ......... Schwifty30 Schwifty31 Schwifty32 .......... 

I want it to actually be numerical based off the number attached to the end. Is there any way to accomplish this using the sort function?

3
  • One answer of that post uses the same option, but that question is more about renaming the files than actually sorting them. I think not duplicate. Commented Jan 21, 2017 at 20:42
  • 2
    There are ways to do it, it all depends on your input. With this particular input sample sort -k1.9,1n should do (that is the sort starting position is the 9th char). If you can't use a starting position then isolating the column to be sorted is the way to go... Commented Jan 21, 2017 at 21:19
  • cut -c9-10 a.txt |sort -n Commented Jan 22, 2017 at 23:11

1 Answer 1

8

Use the version sort option.

sort -V source > destination 
4
  • -V does not appear to be an option for sort on my server Commented Jan 21, 2017 at 20:31
  • 1
    try using the long option --version-sort. Also check what version of sort your are using sort --version. I'm using sort (GNU coreutils) 8.22 Commented Jan 21, 2017 at 20:38
  • claiming that both of those are not legal options Commented Jan 21, 2017 at 21:12
  • 1
    @sharp sort -V works in my Debian 8.5 and does the job you need. It would be better to advise details of your OS & sort version. If your sort does not support operation similar to --version-sort option as explained for example here manpages.debian.org/jessie/coreutils/sort.1.en.html then you need to manipulate your file. Commented Jan 21, 2017 at 22:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.