Natural sort with just Bash or core Linux commands?
How do you sort a directory in the natural sort order just using Bash and core Linux utilities (such as sort and ls)? (In other words, no C, C++, PHP, Perl, etc.)
In natural sort, the file names img0.jpg img1.jpg img10.jpg should appear img0.jpg img1.jpg img10.jpg Instead of the standard sort or even dictionary sort (not helpful): img0.jpg img10.jpg img1.jpg |
Here is an example:
Quote:
Regards, Fordeck |
Rename your files to have leading 0's in the name so that dictionary sort order IS natural sort order.</cheap shot>
Seriously though, I'd like to know this too. |
fordeck: that's nice, but what if your list is like this:
Code:
file0.dat |
Matthew is correct. The program should automatically be able to handle any kind of file such as 123.jpg, Picture123.jpg, img123.jpg, etc. These algorithms exist in C, but as I wrote, I need Bash. :)
My main purpose of this original question is to make it easier to create DVD slide shows from OpenOffice.org Impress (and PowerPoint) using a nice, existing program written in Bash. https://sourceforge.net/tracker/?fun...roup_id=100188 http://www.oooforum.org/forum/viewtopic.phtml?t=45483 |
After googling about a bit I can't find a Free Software tool for this. If you have a C program already written, consider releasing it under the GPL. :D
Seems like a massive omission though. I'm surprised there's no option in the GNU sort program. |
If you've got C, shouldn't it be possible (if painful) to port that to Bash? If you don't want to do the work yourself for whatever reason, then please release the source so that maybe somebody else here can work on it.
I suppose it was omitted because it never came up as an issue. I think most would take the <cheap shot/> approach, or didn't care what order things were in. There's a secondary advantage to <cheap shot/>, it is a lot prettier to ls because everything is nicely aligned. |
Quote:
Quote:
|
Quote:
Hey, this sounds like something cool to do in Lisp. I've been meaning to do something more than "Hello World", and it doesn't look like this has been done before. |
Hi
PHP has this function, so if you have PHP installed, you can use this script: Code:
#!/usr/bin/php If you set execute rights on the script, it will run like every other script, even if its PHP. When running the script, you can use a filename as parameter, if you don't specify it, it will read from stdin. |
Hi.
Given the data file data2: Code:
file0.dat Code:
#!/bin/sh Code:
% ./s1 Not bullet-proof, but good enough for government work ... cheers, makyo ( edit 1: typo; missing single-quote ) |
makyo, that's really neat. I have to spend some time to understand it.
Having said that, it's not a general solution to the natural sort problem. It fails to work correctly the numeric portion precedes the non-numeric part. For example, this list won't be sorted correctly. Code:
02Al002 |
I just found that there is a patch for the GNU sort program:
http://sourcefrog.net/projects/natsort/textutils.diff This adds natural sorting to GNU sort. There is also a stand-along natural sort utility written by the guy who made the patch, called natsort. |
here's a REALLY cheap way to do this:
Code:
#!/bin/bash |
here's a REALLY cheap way to do this
Actually its rather expensive cuz it won't work: you'll be missing the header file. |
I think the options are (in descending order of probable (usefulness * 1/hassle) / 2):
The patch for GNU sort from the guy who did natsort is out of date. I talked to the folks in #gnu at freenode, and they said they'd probably consider committing the feature if someone can implement it neatly, and use a nice long option name like --natural-sort (it doesn't deserve a short option apparently... :) ) |
All times are GMT -5. The time now is 09:28 PM. |