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.
For a better explanation of the basic principle see this Wiki article.
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:
03.longclip.jpg
clip 03.jpg
clip.jpg
03 clip.jpg
03.jpg
Water 06.wav
Water 01.wav
clip.03.jpg
03.clip.jpg
where you want to treat everything up to the last dot as the filename root.
This can be easily achieved by making the regex greedy, that is, changing it from ([^.]+) to (.+)\.
Sorted output:
03.jpg
03 clip.jpg
03.clip.jpg
03.longclip.jpg
clip.jpg
clip 03.jpg
clip.03.jpg
Water 01.wav
Water 06.wav
I tried to duplicate this with the sort program but I couldn't find a way to make it count the positions from the end of the string. Something like -k-2,-2 doesn't work.
This is not a Perl thingy. Any mature language should be able to accomplish that. The ST Wiki article gives some hints, I haven't tried all that, though.
Anyways, you receive my personal Thanks and Kisses for posting this line:
my $pattern = $ENV{KMVAR_Pattern};
For some obscure reasons I always used to get KM variables into Perl via osascript […] getvariable […]
Your variant (via env) is much more clean, and I have no clue why it didn't occur to me earlier
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.
I often use this method:
##Macro Library COPY with Selection Test [Sub-Macro]
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/f/f31e210b0732821c0fa13a869eb0eb0285346066.kmmacros">COPY with Selection Test [Sub-Macro].kmmacros</a> (6.3 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
###ReleaseNotes
1. Call this Sub-Macro in an Execute Macro Action
2. Then use a IF/THEN Action to test for KM Variable "CBS__Clipboard_Changed"
= 1 IF the Clipboard has changed
---
<img src="/uploads/default/original/2X/e/e143f08c6c0dec941e48b7a7c667f95290b9095c.png" width="595" height="953">
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.