Hi guys, I’m brand spanking new to RegEx and if anyone is willing I could use a hand doing the seemingly simple task of sorting text (where the source is separated by a carriage return), ascending. I’ve been attempting this all morning and my newbiness points to asking for help. I don’t need it to be fancy at this point, but in the following sample data, ideally text that starts with a number would be at the top, and then text that has trailing numbers would sort by the leading text first, and then those trailing numbers:

clip 03.jpg
clip.jpg
03 clip.jpg
Water 06.wav
Water 01.wav

So ideally the result of this example would be:

03 clip.jpg
clip.jpg
clip 03.jpg
Water 01.wav
Water 06.wav

But honestly, I’ll take any help as a stepping stone to learning RegEx.

Wrong tool. Regexp doesn’t sort. But the command line tool “sort” with the -f option does (or you can use BBEdit’s sort to do it). In Keyboard Maestro use the Execute A Shell Script action to “sort -f input.txt -o output.txt” with the -f ignoring case.

This looks more complicated, but what the script does is basically just this:

It creates a list from the input, where each line is a list element (e.g. “clip.jpg”).

It converts each list element into an “array”, where the first element is the complete list element itself (“clip.jpg”) and the second element is only the filename part before the dot (“clip”), which is the part that is actually relevant for sorting.

The second element is created with the regex ([^.]+)

Now the list is being sorted according to the lowercase variants of the second element of each array (“clip”, “clip 03”, etc.).

After sorting the second part of each list element is stripped, as it is no longer needed.

Finally the sorted list is converted back into individual lines again.

I should really have tested my proposed solution. As you pointed out, the period before the file extension makes it tricky. Periods (46) are farther down the ASCII list than a Space (32) so spaces take precedence in an ASCII sort.

For my punishment, I submit this macro to sort a selection of lines in a text editor. I didn’t resort to the Schwartzian Transform, though. I just told old Unix sort to ignore case and use the period as a delimiter, sorting the first field (delimited by the period) and then sort the next field.

echo "$KMVAR_kmVar" | sort -f -t'.' -k1,1 -k1,2

There are some cases where you’d want to look past the first field (just add ‘Water 06.jpg’ to that list, for example). The ST solution fails there but this one handles that.

Sometimes it seems like every list needs custom code to sort. But I’m going to try using this macro generally for a while to see when it breaks. I’m sure it will.

Anyway, I learned something following you on this topic and wanted to say thanks.

Hey @mrpasini, that is a nice one and I think it deserves the award “Most elegant solution” in the sense that it gets the job done without adding complexity. Also using the extension as a second key is a nice option, if desired.

That being said, the ST has one advantage: Since it is using a regular expression to determine the search key (the part in-between the slashes) it is more flexible than counting the key delimiter positions.

This allows you to handle also cases – for example – with a varying number of “fields”. Let’s say you have this:

I thought it would be nice to have a sort macro that lets you experiment with the regexp pattern in the Schwartzian Transform. So here it is

Thanks for sharing. That looks very useful.

Here’s a few comments on your macro:

Unfortunately, all apps do not handle the “Copy” menu item appropriately, and leave it enabled even if nothing is selected, so you may want to use another method to determine if someething is selected.

The KM Action “Copy” will fail if nothing is selected, so you could test for success of that action.

Unfortunately, all apps do not handle the “Copy” menu item appropriately, and leave it enabled even if nothing is selected, so you may want to use another method to determine if someething is selected.

I guess the problem is beyond me, but for testing different regexen just use something like this:

I recall reading about that problem with the Edit menu from a while ago. And I told myself if I ever ran into it, I’d use your solution, preferring the simpler menu check (you know, as long as it works).

But thanks. I just implemented it in a test macro so it’s handy when I need it. And it isn’t noticeably slower, either. But then I was able to turn off the pause entirely.

If you are referring to the Pause after the ⌘C, then that can be dangerous.
Often the app needs some time AFTER the Copy command to complete the operations. Without the pause you could end up without anything on the Clipboard.