# Sorting alphanumeric strings without a pattern

**URL:** <https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314>\
**Category:** Shell Programming and Scripting\
**Created:** [June 4, 2012, 4:27pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314 "2012-06-04T16:27:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 4, 2012, 4:27pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/1 "2012-06-04T16:27:24Z")

</div>

Good evening to all!!  
I'm facing this problem:

I saved in a txt a list of files name (one txt for every folder):

hello0.jpg  
hello1.jpg  
hello10.jpg  
hello11.jpg  
hello12.jpg  
hello13.jpg  
hello14.jpg  
hello15.jpg  
hello16.jpg  
hello17.jpg  
hello18.jpg  
hello19.jpg  
hello2.jpg  
hello20.jpg  
hello21.jpg  
hello22.jpg  
hello23.jpg  
hello24.jpg  
hello3.jpg  
hello4.jpg  
hello5.jpg  
hello6.jpg  
hello7.jpg  
hello8.jpg  
hello9.jpg

and I would like to sort them using the numbered part.  
Using sort -n I get the aforementioned sorting.  
The real problem is that I don't have a fixed pattern for files name (otherwise I could use sort with -k).  
It could be:

hello0.jpg  
helloagain0.jpg  
hello1\_0.jpg

Only thing I know is that all files in the same folder will have same pattern.

Any help is appreciated!!

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/6_449c533f586939b6e5f246632a46421f.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [June 4, 2012, 4:50pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/2 "2012-06-04T16:50:56Z")

</div>

One can extract the numeric part, paste them together and sort by newly added column, then get rid if the added column on the output:

```nohighlight
$ cat aa
qwe1.asd
zxc3.ewq
wer4.tre
qweqwe2.asd
$
$ cat aa|tr -d '[:alpha:]'|paste aa - | sort -k2,2n | cut -f1
qwe1.asd
qweqwe2.asd
zxc3.ewq
wer4.tre
$ 

```

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 5, 2012, 6:05am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/3 "2012-06-05T06:05:08Z")

</div>

Thanks a lot for your super fast reply!!  
Does this work even with this filename:

JonnyDouble01\_pg001.jpg  
JonnyDouble01\_pg002.jpg  
.....

where there are more than one numeric part (one is the same for all files, the other is the key to sorting...)

---------- Post updated 06-05-12 at 05:05 AM ---------- Previous update was 06-04-12 at 04:01 PM ----------

Using these files, I can't get to sort them:

h0ello0.jpg  
h1ello1.jpg  
h2ello10.jpg  
h3ello11.jpg  
h4ello12.jpg  
h5ello13.jpg  
h6ello14.jpg  
h7ello15.jpg  
h8ello16.jpg  
h9ello17.jpg  
h10ello18.jpg  
h1ello19.jpg  
h12ello2.jpg  
h13ello20.jpg  
h14ello21.jpg  
h15ello22.jpg  
h16ello23.jpg  
h17ello24.jpg  
h18ello3.jpg  
h19ello4.jpg  
h20ello5.jpg  
h21ello6.jpg  
h22ello7.jpg  
h23ello8.jpg  
h24ello9.jpg

That's because they have more than one numeric part.  
Only common things among all files is that the are numbered in the end of the name (before the extension).

Only way I found to srt them is to remove the extension (.jpg or whatever), than read one char at a time from the end of each string and stop when the char isn't a number.

That's the starting point of the numeration and I can use sort -k option...

Now I need to find correct commands!😃

---

<div class="post-metadata">

**Author:** ![elixir\_sinari](https://community.unix.com/user_avatar/community.unix.com/elixir_sinari/32/2563_2.png) [@elixir\_sinari](https://community.unix.com/u/elixir_sinari)\
**Post date:** [June 5, 2012, 6:19am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/5 "2012-06-05T06:19:17Z")

</div>

A crude way:

```nohighlight
sed 's/.*[^0-9]\([0-9]*\)\..*/\1/g' <filename>|paste - <filename>|sort -nk1|awk '{print $2}'

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 5, 2012, 6:25am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/6 "2012-06-05T06:25:29Z")

</div>

Slightly less crude 😉

```nohighlight
sed 's/.*[^0-9]\([0-9]*\)\./\1 &/' file | sort -n | cut -d" " -f2-

```

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 5, 2012, 6:55am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/7 "2012-06-05T06:55:22Z")

</div>

That really helped me!!!!  
Thanks a lot to everyone.  
Last question: are you aware of any limitation of this approach? i.e. any file name it can't sort correctly?

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 5, 2012, 7:08am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/8 "2012-06-05T07:08:51Z")

</div>

The file names need to have an extension, or it will not work..  
Here is an alternative sed that should work for any pattern, you could try:

```nohighlight
sed 's/.*[^0-9]\([0-9][0-9]*\)[^0-9]*$/\1 &/'

```

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [June 5, 2012, 7:16am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/9 "2012-06-05T07:16:01Z")

</div>

```nohighlight
# awk '{s=gensub("[^0-9]*([0-9][0-9]*).*","\\1",$0);print s,$0}' infile|sort -n|awk '{print $2}'

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 5, 2012, 7:32am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/10 "2012-06-05T07:32:20Z")

</div>

@ygemici, that will not work for file names with spaces and/or with multiple number sequences in them... gensub is gawk only..

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [June 5, 2012, 8:55am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/11 "2012-06-05T08:55:32Z")

</div>

> [@scrutinizer](#):
>
> @ygemici, that will not work for file names with spaces and/or with multiple number sequences in them... gensub is gawk only..

```plaintext
# awk '{x=$0;gsub("[^0-9]"," ");print $NF,x}' infile|sort -n|awk '{$1="";sub(" ","")}1'

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 5, 2012, 9:27am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/12 "2012-06-05T09:27:49Z")

</div>

Yup that would not have those shortcomings I think 🙂

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [June 5, 2012, 10:34am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/13 "2012-06-05T10:34:49Z")

</div>

> [@scrutinizer](#):
>
> Yup that would not have those shortcomings I think 🙂

🙂 🙂

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 22, 2012, 5:05am UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/14 "2012-06-22T05:05:12Z")

</div>

Thanks again for your help!!!

I tried with severl filenames but I encountered one problem!

With these files:

03\_000a.jpg  
03\_000b.jpg  
03\_000c.jpg  
03\_001.png  
03\_002.png  
03\_003.png  
03\_004-005.png  
03\_006.png  
03\_007.png  
03\_008.png  
03\_009.png  
03\_010.png  
Credits10.png  
Credits11.png

I get this sorting:

03\_000a.jpg  
03\_000b.jpg  
03\_000c.jpg  
03\_001.png  
03\_002.png  
03\_003.png  
03\_004-005.png  
03\_006.png  
03\_007.png  
03\_008.png  
03\_009.png  
03\_010.png  
Credits10.png  
03\_011.png  
Credits11.png  
03\_012.png  
03\_013.png

Because of this problem (first numbers are the sorting order).

000 03\_000a.jpg  
000 03\_000b.jpg  
000 03\_000c.jpg  
001 03\_001.png  
002 03\_002.png  
003 03\_003.png  
005 03\_004-005.png  
006 03\_006.png  
007 03\_007.png  
008 03\_008.png  
009 03\_009.png  
010 03\_010.png  
011 03\_011.png  
012 03\_012.png  
013 03\_013.png  
10 Credits10.png  
11 Credits11.png

I think the problem is that filenames aren't consistent in this folder.  
Is it possible to reproduce the "sort by name" windows option?

Thanks again!!

EDIT: probably here is a solution:

1) get a column of every first character using

```nohighlight
sed 's/\(.\{1\}\).*/\1/' file1

```

2) get a column of incremental numbers just like I already do using

```nohighlight
sed 's/.*[^0-9]\([0-9][0-9]*\)[^0-9]*$/\1 &/' file2

```

(although I need to get only the first column)  
3) sort the lines using two keys: first one should be file1, second one should be file2

this way, I can sort twice, once alphabetically, then, keeping the first sorting result, using incremental numbers from files name...  
I think this should work, but I don't know how to do it...

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [June 22, 2012, 12:37pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/15 "2012-06-22T12:37:43Z")

</div>

Do you know perl...because with perl you can split and store filenames in a hash array and sort them using either the key which can be the name...or the value which would be the numeric part of the filename or on both the key and value...so you have a lot of options to play with in perl...

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 22, 2012, 12:45pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/16 "2012-06-22T12:45:40Z")

</div>

Sadly, I need plain unix as I'm using it in a Kindle!

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 22, 2012, 12:55pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/17 "2012-06-22T12:55:49Z")

</div>

what is "plain unix"?

What kind of "plain unix" runs in a kindle?

There is no actual operating system named "UNIX" any more, the same way IEEE doesn't actually manufacture electrical sockets, just define what they ought to look like.

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 22, 2012, 1:09pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/18 "2012-06-22T13:09:37Z")

</div>

As I stated before, I'm new to all of this...  
All I can tell you is that I can't use perl or bash, only "standard" shell commands.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 22, 2012, 1:20pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/19 "2012-06-22T13:20:39Z")

</div>

I'm not trying to be difficult, my question's actually relevant. If you don't know, you should find out -- it's important. Even the 'standard' commands aren't quite the same everywhere, and knowing what extensions you do and don't have may make the difference between a simple, fast solution and a slow, difficult one.

`uname -a` should tell you.

---

<div class="post-metadata">

**Author:** ![silver18](https://community.unix.com/letter_avatar/silver18/32/6_449c533f586939b6e5f246632a46421f.png) [@silver18](https://community.unix.com/u/silver18)\
**Post date:** [June 22, 2012, 1:21pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/20 "2012-06-22T13:21:13Z")

</div>

Here's the output:

```nohighlight
Linux kindle 2.6.31-rt11-lab126 #1 Wed Apr 4 20:41:38 PDT 2012 armv7l GNU/Linux

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 22, 2012, 1:25pm UTC](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314/21 "2012-06-22T13:25:45Z")

</div>

Okay, you have linux on an embedded system, which probably means your utilities -- close to all of them -- are just busybox. (a swiss-army-knife of a program which can pretend to be dozens or hundreds of different minimal shell commands.) This isn't generic UNIX at all, quite far from it really.

busybox comes with a half-decent version of awk at least so I'll take a look...

[Next page](https://community.unix.com/t/sorting-alphanumeric-strings-without-a-pattern/311314.md?page=2)
