Numerically sort clipboard contents

Using Peter's Bash script from above, this should do the trick:

Example Output:

image


MACRO:   Sort Numbers on Clipboard and Format Output [Example] @Bash


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/c/5/c54e84508977cf0674da8ff602de29cd17ca4ab9.kmmacros">Sort Numbers on Clipboard and Format Output [Example] @Bash.kmmacros</a> (3.0 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|489x563](upload://d1kgozmfslt2XfWyQYeJDFWZWSl.jpg)

I would also use "␣". Or, maybe easier to understand: "<space>"

Treating the stuff in-between the numbers as field separators (what they actually are), you can use Awk instead of regex replacements:

sort -n | awk  '$1=$1'  RS="" FS="\n" OFS=", "

This also eliminates the problem with the trailing comma, since awk automatically ignores any (superfluous) field separator at the end of the record.


Explanation:

With the variables at the end you set the different separators:

  • RS ‘Record Separator’: empty, because we don’t have any (it’s one record that ends with the end of text)
  • FS ‘[the original] Field Separator’: line feed
  • OFS ‘Output Field Separator’: the desired comma plus space (", ")

$1=$1 seems to be needed to force awk to reevaluate the fields.

A print is not needed since by default awk prints all fields. So, what awk actually does, is simply this:

  1. Learning the existing separators (RS, FS)
  2. Returning the whole record using the new field separator (OFS)

More info

2 Likes

awk is my favourite instrument in the shell cabinet – always repays a little experimentation.

See - Effective Awk Programming – 4th edition

1 Like

Wow! I have rarely, if ever, used awk, but it is evidently a major tool, with over 500 pages in its PDF manual:

GNU implementation: gawk

If you are like many computer users, you would frequently like to make changes in various text files wherever certain patterns appear, or extract data from parts of certain lines while discarding the rest. To write a program to do this in a language such as C or Pascal is a time-consuming inconvenience that may take many lines of code. The job is easy with awk, especially the GNU implementation: gawk.

Does the macOS use "GNU awk"?

The amount of pages in a manual is rarely an indicator of the power or the usefulness of a tool :wink:
Awk shines when it comes to structured data, or better: data that can be structured. Recognizing fields in an record and then attemptimg to work with the fields (which includes the separators) is always better than treating the separators as mere strings and brute-forcely replacing them via regex. I think.

But, I think, you can achieve the same with Perl without regexes (i.e. only by manipulating separators). Beyond my current Perl knowledge :wink:

Concerning the gawk on macOS, I’m not sure. Most likely macOS has just awk, who knows which version. Have to look it up. I got a related problem here.


Edit:

No, gawk, as expected, comes only when installed via Homebrew or similar. But I don’t know the differences between gawk and awk, and I don’t know if awk/gawk scripts tend to use only the specific implementation (gawk or awk) or if they generously default to the one that is installed. (I have no practice with awk scripting.)

1 Like

To pick up a part of your quote:

If you are like many computer users, you would frequently like to make changes in various text files wherever certain patterns appear, or extract data from parts of certain lines while discarding the rest.

That’s what I meant with “data that can be structured”. I tend to use Awk for that. Although “in general” I prefer Perl for text (not for data) manipulation. But applying regexes while clearly a data structure is present (delimeters, fields, etc.) feels a bit clunky/forced/out-of-place. But I’m sure Perl also has Awk-like capabilities (if not better!) to handle fields and such. I just don’t know them.

1 Like

Yes, I started out (ie, several decades ago) using sed & awk for text processing. But since perl does everything they do and more, I tend to use perl directly instead of sed or awk for most tasks.

For example, a close approximation to @Tom’s:

awk  '$1=$1'  RS="" FS="\n" OFS=", "

Might be something like this:

perl -e 'use English; $OFS = ", "; @F = <>; chomp(@F); print @F'

But in perl you would probably write it more like this:

perl -e '@F = <>; chomp(@F); print join( ", ", @F )'

Basically, sed and awk are very useful tools, but they tend to be limited in what they can do, and when you hit the limit then you have to start again from scratch, which is why I tend to just use perl, which can do the sort of text processing that sed and awk can do, but then extends out to anything else as needed.

But just like @ComplexPoint reaches for JavaScript and @ccstone reaches for AppleScript, we all tend to use whatever tools we are most familiar with to solve any given task.

3 Likes

Yep, something like that is what I had in mind as I said “But I’m sure Perl also has Awk-like capabilities (if not better!) to handle fields and such. I just don’t know them.” :slight_smile:

Thanks for showing. Perl is just awesome!

2 Likes

Actually my first effort was:

read -r -d '' numList <<'EOF'
77
74
32
EOF

echo "$numList" | sort -n | awk 'BEGIN {RS=""}{gsub(/\n/,", ",$0);print $0};'

Result  -->  32, 74, 77

:sunglasses:

But here's why I would normally reach for AppleScript and the Satimage.osax:

----------------------------------------------------------------
# REQUIRES: Satimage.osax --> http://tinyurl.com/satimage-osaxen
# AppleScript and the Satimage.osax are fully Unicode-aware.
----------------------------------------------------------------
# Make sure the clipboard has what we want on it:
set the clipboard to text 2 thru -2 of "
77
74
32
"
----------------------------------------------------------------
--» Main
----------------------------------------------------------------
set numList to join (sortlist (find text "\\d+" in (get the clipboard) with regexp, all occurrences and string result) comparison 2) using ", "
----------------------------------------------------------------

--> "32, 74, 77"

----------------------------------------------------------------

I really like sed and awk, but they are fairly obsolete (GNU sed and GNU awk notwithstanding), because they don't handle Unicode text very adroitly.

So – my recommendation is:

Learn as much sed and awk as you want – particularly snippets that are useful.

But if you want to seriously study something then study Perl. It's Unicode-aware and many times more powerful than sed and awk combined.

-Chris

2 Likes

Updated 2018/08/11 18:50 CDT
Fixed the problem Peter noted in post #27.


Hey Folks,

Might as well post a Perl-only solution.

This one will remove any blank lines in the imput.

-Chris


Sort Numeric Column -- Transform to '- ' Separated Values.kmmacros (5.4 KB)

I believe Perl’s sort sorts alphabetically, so that would sort 100 before 22. You need to add { $a <=> $b } or something like that to the sort.

Hey Peter,

Bleep! You're quite right.

Thanks – I'll fix that later today.

Right now I'm knackered and require a nap. 😴

-Chris

Of all of the bash solutions, this script is the most readable to me.
Of course, readability depends in large part on the reader's knowledge of the language, and I'm clearly a novice at bash.

But I do enjoy RegEx, so it is fairly easy for me to read this script, and make changes to it if need be.

However, we have a number of good choices posted here, so you can pick the one whose language you understand/like the best.

You find

sort -n | perl -pe 's/\n/, /' | perl -pe 's/, $//'

more readable than

printf '%s, ' $(cat | sort -n) | rev | cut -c 3- | rev

?

I don't feel a Satimage scripting addition was especially needed in this situation:

set the clipboard to "
77
74
32
"

set my text item delimiters to {", ", linefeed}
text items of (the clipboard)'s words as text

Hey @CJK,

I didn't say it was, but your script doesn't sort the numbers – mine does.

Mine will also scrape numbers out of other text and is therefore a bit more versatile.

-Chris

Good point. I jumped in too early thinking that it was just the latter part of the puzzle we were solving again (i.e. concatenation). Egg on my face.

1 Like

Yes. I thought I explained why. I already know some RegEx so it is easy for me to read the perl commands. Whereas I don't know any of the other commands you used (other than sort).

For those that don't know RegEx, the perl probably looks like gibberish. :wink:

BTW, I didn't say one was better than the other -- just which one was more readable to me. YMMV.

Hey Folks,

I've update my macro in post #26 to fix the issue Peter noted in post #27.

Lexical sort changed to ascending numeric sort.

-Chris