How to randomize a list?

476 views
Skip to first unread message

Vlad Ghitulescu

unread,
Jan 13, 2018, 2:47:00 PM1/13/18
to BBEdit Talk
Hello!


I have a list of difficult words / phrases for my son (see below) that I
want to randomize in order to keep the boy awake :-)

I've found similar (old!) questions:

http://bbedit-talk.barebones.narkive.com/jXrdz0T6/randomize-lines

https://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-lines-of-a-text-file-on-the-unix-command-line-or-in-a-shel

https://stackoverflow.com/questions/886237/how-can-i-randomize-the-lines-in-a-file-using-standard-tools-on-red-hat-linux

https://superuser.com/questions/760732/randomly-shuffle-rows-in-a-large-text-file

but I couldn't find out how to make them work in BBEdit (so I ended
using http://www.randomizelist.com until I get a hint from you ;-).

Could you please help me?

Thanks!


Regards,
Vlad


P.S. Here's the current list:

ähnlich
Antrieb
auspacken
blitzschnell
Brot
Brücke
Bruder
Brunnen
Bus
diesem
Dreieck
Eichhörnchen
Ellenbogen
er hat
Erfolg
Erlebnis
erschrecken
erzählen
Fahrzeug
fallen
feuerspeiend
Flicken
Förster
Fritz
Fußball
Gefahr
Glück
groß
Hals
hässlich
hell
hellblau
hellbraun
hellgelb
hellgrün
hellorange
hier
ich heiße
ich wünsche mir
Idee
ihm
im Gesicht
in der letzten Reihe
Kinderkramm
Kiste
knacken
können
Leggings
lieb
lieber
Lippen
Mädchen
man kann
Märchen
Maschine
Mitte
Nachmittag
nichts wie hin
ohne
Ohren
Platz
Prinz
Pullover
Pumpe
Punkte
quer
Rand
Rasen
rechts
schlau
Schloss
schützen
schwarz
schwimmen
sehen
Seite
sie
sie sieht aus
sitzen
sogar
Sommersprossen
Sonne
Spaß
spielen
Staub
Straße
Tier
Tropfen
tun
verrückt
verstecken
viel
voll
von
vor
wartet
Wasser
Werkstatt
Werkzeug
zusammen

Sam Hathaway

unread,
Jan 13, 2018, 6:50:02 PM1/13/18
to BBEdit Talk

Make a text filter called Randomize with the contents:

#!/bin/sh
exec shuf

Text filters go in ~/Library/Application Support/BBEdit/Text Filters. I think they probably have to be executable. Running these command in Terminal should do the trick:

cd ~/Library/Application\ Support/BBEdit
mkdir -p Text\ Filters
cd Test\ Filters
echo -e "#!/bin/sh\necho shuf" > Randomize
chmod +x Randomize

You should then see it in the “Apply Text Filter” submenu of the “Text” menu.

Hope this helps!
-sam

--
This is the BBEdit Talk public discussion group. If you have a feature request or would like to report a problem, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: http://www.twitter.com/bbedit
--- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To post to this group, send email to bbe...@googlegroups.com.
Visit this group at https://groups.google.com/group/bbedit.

Jean-Christophe Helary

unread,
Jan 14, 2018, 2:47:10 AM1/14/18
to bbe...@googlegroups.com
shuf requires coreutils which is not in macOS by default. You can install it with homebrew.

sort -R seems to be available in recent versions of macOS (the man page indicates a 2015 date).

There is also a solution with Perl as given here:

https://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-lines-of-a-text-file-on-the-unix-command-line-or-in-a-shel

Jean-Christophe
Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com


Vlad Ghitulescu

unread,
Jan 14, 2018, 3:22:06 PM1/14/18
to BBEdit Talk

Hi, Sam!

Thanks for the explanations & reminder (I think Chris was so nice to show me this once - because I still have two TextFilters - but I didn't use it since then and I've forgot :-((()

It seems that shuf is not there, because everything I've got is this error message:

/Application Support/BBEdit/Text Filters/Randomize.sh: line 2: exec: shuf: not found

that replace my active list :-)

I'll try it now with sort -R and report the results.

Until next time,
Vlad

Vlad Ghitulescu

unread,
Jan 14, 2018, 3:25:16 PM1/14/18
to bbe...@googlegroups.com

Hi, Jean-Christophe!

sort -R brings this error message:

sort: invalid option -- R
Try `sort --help' for more information.

and sort --help brings this:

--

Vlads27iMac2012:~ Vlad$ sort --help
Usage: sort [OPTION]... [FILE]...
Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too.
Ordering options:

-b, --ignore-leading-blanks ignore leading blanks
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < JAN' < ... <DEC'
-n, --numeric-sort compare according to string numerical value
-r, --reverse reverse the result of comparisons

Other options:

-c, --check check whether input is sorted; do not sort
-k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)
-m, --merge merge already sorted files; do not sort
-o, --output=FILE write result to FILE instead of standard output
-s, --stable stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE use SIZE for main memory buffer
-t, --field-separator=SEP use SEP instead of non-blank to blank transition
-T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp;
multiple options specify multiple directories
-u, --unique with -c, check for strict ordering;
without -c, output only the first of an equal run
-z, --zero-terminated end lines with 0 byte, not newline
--help display this help and exit
--version output version information and exit

POS is F[.C][OPTS], where F is the field number and C the character position
in the field. OPTS is one or more single-letter ordering options, which
override global ordering options for that key. If no key is given, use the
entire line as the key.

SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.

Report bugs to bug-co...@gnu.org.
Vlads27iMac2012:~ Vlad$

--

Do I miss something?

Regards,
Vlad

Sam Hathaway

unread,
Jan 14, 2018, 4:31:22 PM1/14/18
to bbe...@googlegroups.com

So sorry for leading you astray with shuf. I’ve had GNU Coreutils installed for so long that I’ve forgotten what’s in stock macOS.

It sounds like your sort is too old to have -R.

Jean-Christophe’s Perl solution would probably be easiest at this point.

Try replacing the contents of your “Randomize” text filter with:

#!/usr/bin/env perl
use List::Util 'shuffle';
print shuffle(<STDIN>);

Hope this helps!
-sam

Vlad Ghitulescu

unread,
Jan 15, 2018, 12:46:15 AM1/15/18
to bbe...@googlegroups.com

Hi, Sam!

Thanks for staying with me! :-)

On 14 Jan 2018, at 22:31, Sam Hathaway wrote:

So sorry for leading you astray with `shuf`. I’ve had GNU Coreutils installed for so long that I’ve forgotten what’s in stock macOS.

(sorry for the following dumb questions… I've used Linux last time in 1993! :-)
What are the GNU Coreutils good for?
Should I install them too? (I've read about the difference in the performance of the various randomizer solutions in the previous cited StackOverflow-threads)
And if so, how?

It sounds like your `sort` is too old to have `-R`.

That means that the "stock macOS" on my Sierra 10.12.6 are older than… what?

Jean-Christophe’s Perl solution would probably be easiest at this point.

Try replacing the contents of your “Randomize” text filter with:

```
#!/usr/bin/env perl
use List::Util 'shuffle';
print shuffle(<STDIN>);
```

Hope this helps!

Yes, that helped indeed!

That worked even whitout making the file Randomize.sh executable with chmod +x Randomize!
Take a look:

Vlads27iMac2012:Text Filters Vlad$ ls -al
total 48
drwxr-xr-x@ 5 Vlad staff 170 14 Jan 21:17 .
drwxr-xr-x@ 17 Vlad staff 578 15 Jan 06:44 ..
-rwxr-xr-x@ 1 Vlad staff 111 1 Nov 2016 Delete_Viewed_Video_in_Lynda_TOC.pl
-rwxr--r--@ 1 Vlad staff 69 15 Jan 06:32 Randomize.sh
-rwxr-xr-x@ 1 Vlad staff 80 4 Nov 2015 Selection_To_Numbered_List.sh

How is this possible?

Anyway: Thanks again!

-sam

Jean-Christophe Helary

unread,
Jan 15, 2018, 7:58:49 AM1/15/18
to bbe...@googlegroups.com


> On Jan 15, 2018, at 14:46, Vlad Ghitulescu <Vl...@Ghitulescu.de> wrote:
>
> What are the GNU Coreutils good for?

They are common Linux utilities of which some BSD equivalents are present on macOS.

https://www.gnu.org/software/coreutils/manual/coreutils.html

> Should I install them too? (I've read about the difference in the performance of the various randomizer solutions in the previous cited StackOverflow-threads)
> And if so, how?

homebrew

https://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities

> That means that the "stock macOS" on my Sierra 10.12.6 are older than… what?

:) High Sierra ?

I never used sort -R before, but as I wrote the manual has 2015 as a date and Sierra was released in 2016, so I guess Apple saw fit to upgrade sort for High Sierra.

Would you mind checking the date at the end of the sort man page on your machine?

Sam Hathaway

unread,
Jan 15, 2018, 9:32:54 AM1/15/18
to bbe...@googlegroups.com

On 15 Jan 2018, at 0:46, Vlad Ghitulescu wrote:

(sorry for the following dumb questions… I've used Linux last time in 1993! :-)
What are the GNU Coreutils good for?

Should I install them too? (I've read about the difference in the performance of the various randomizer solutions in the previous cited StackOverflow-threads)
And if so, how?

They provide roughly the same functionality as the standard UNIX utilities that come with macOS. (Which come from a BSD lineage.) I like them because I used Linux for years and get tripped up by the differences in the command-line interfaces. If you don’t have this problem, then I don’t think there’s a good reason to install the GNU versions.

The Perl solution should be fine for your purposes. It will randomize correctly (unlike sort -R, which actually sorts by an arbitrary, but not random, characteristic of the data), and performance should be acceptable.

That worked even whitout making the file Randomize.sh executable with chmod +x Randomize!
Take a look:

Vlads27iMac2012:Text Filters Vlad$ ls -al
total 48
drwxr-xr-x@ 5 Vlad staff 170 14 Jan 21:17 .
drwxr-xr-x@ 17 Vlad staff 578 15 Jan 06:44 ..
-rwxr-xr-x@ 1 Vlad staff 111 1 Nov 2016 Delete_Viewed_Video_in_Lynda_TOC.pl
-rwxr--r--@ 1 Vlad staff 69 15 Jan 06:32 Randomize.sh
-rwxr-xr-x@ 1 Vlad staff 80 4 Nov 2015 Selection_To_Numbered_List.sh

How is this possible?

See the x in column 4? That indicates that Randomize.sh is in fact executable, by the owner (you). BBEdit also runs as you, so it can execute the script just fine.

Just a note: BBEdit doesn’t care, but to avoid future human confusion you might want to change Randomize.sh to Randomize.pl since it’s a Perl script now.
-sam

Christopher Stone

unread,
Jan 16, 2018, 2:51:28 AM1/16/18
to BBEdit-Talk
On 01/14/2018, at 15:31, Sam Hathaway <list....@munkynet.org> wrote:
Try replacing the contents of your “Randomize” text filter with:
#!/usr/bin/env perl
use List::Util 'shuffle';
print shuffle(<STDIN>);


Hey Vlad,

I'm with Sam, although I'd write it slightly differently.

#!/usr/bin/env perl -sw
use List::Util 'shuffle';
print shuffle <>;

The main thing here is to turn on the strict and warnings switches, although I've also abbreviated STDIN and used a less formal call to shuffle().

--
Take Care,
Chris


Vlad Ghitulescu

unread,
Jan 16, 2018, 7:36:56 AM1/16/18
to bbe...@googlegroups.com


On 15 Jan 2018, at 15:33, Sam Hathaway wrote:

>> That worked even whitout making the file Randomize.sh executable with
>> chmod +x Randomize!
>> Take a look:
>>
>> Vlads27iMac2012:Text Filters Vlad$ ls -al
>> total 48
>> drwxr-xr-x@ 5 Vlad staff 170 14 Jan 21:17 .
>> drwxr-xr-x@ 17 Vlad staff 578 15 Jan 06:44 ..
>> -rwxr-xr-x@ 1 Vlad staff 111 1 Nov 2016
>> Delete_Viewed_Video_in_Lynda_TOC.pl
>> -rwxr--r--@ 1 Vlad staff 69 15 Jan 06:32 Randomize.sh
>> -rwxr-xr-x@ 1 Vlad staff 80 4 Nov 2015 Selection_To_Numbered_List.sh
>>
>> How is this possible?
>
> See the `x` in column 4? That indicates that `Randomize.sh` is in
> fact executable,  _by the owner_  (you). BBEdit also runs as you, so
> it can execute the script just fine.

Why do the others have an x on column 7 (that's groups, right?!)?


> Just a note: BBEdit doesn’t care, but to avoid future human
> confusion you might want to
> change `Randomize.sh` to `Randomize.pl` since it’s a Perl script
> now.

Done.

Thanks again!

Vlad Ghitulescu

unread,
Jan 16, 2018, 7:38:55 AM1/16/18
to BBEdit-Talk


On 16 Jan 2018, at 8:51, Christopher Stone wrote:

> On 01/14/2018, at 15:31, Sam Hathaway
> <[list....@munkynet.org](<mailto:list....@munkynet.org>)> wrote:
>
>> Try replacing the contents of your “Randomize” text filter with:
>>
>>
>>
>> #!/usr/bin/env perl
>> use List::Util 'shuffle';
>> print shuffle(<STDIN>);
>>
>>
>
> * * *
>
> Hey Vlad,
>
> I'm with Sam, although I'd write it slightly differently.
>
> #!/usr/bin/env perl -sw
>
> use List::Util 'shuffle';
>
> print shuffle <>;
>
> The main thing here is to turn on the  _strict_  and  _warnings_
>  switches, although I've also abbreviated STDIN and used a less
> formal call to shuffle().

:-D

I've kept the best of both worlds and added -sw to Sam's Randomize.

Jean-Christophe Helary

unread,
Jan 16, 2018, 7:58:06 AM1/16/18
to bbe...@googlegroups.com


> On Jan 16, 2018, at 21:36, Vlad Ghitulescu <Vl...@Ghitulescu.de> wrote:
>
>>> drwxr-xr-x@ 5 Vlad staff 170 14 Jan 21:17 .
>>> drwxr-xr-x@ 17 Vlad staff 578 15 Jan 06:44 ..
>>> -rwxr-xr-x@ 1 Vlad staff 111 1 Nov 2016 Delete_Viewed_Video_in_Lynda_TOC.pl
>>> -rwxr--r--@ 1 Vlad staff 69 15 Jan 06:32 Randomize.sh
>>> -rwxr-xr-x@ 1 Vlad staff 80 4 Nov 2015 Selection_To_Numbered_List.sh
>>>
>>> How is this possible?
>>
>> See the `x` in column 4? That indicates that `Randomize.sh` is in fact executable, _by the owner_ (you). BBEdit also runs as you, so it can execute the script just fine.
>
> Why do the others have an x on column 7 (that's groups, right?!)?

Because the group (staff) has read and execute (search for directories) access on them.

Vlad Ghitulescu

unread,
Jan 16, 2018, 8:02:07 AM1/16/18
to bbe...@googlegroups.com


On 16 Jan 2018, at 13:57, Jean-Christophe Helary wrote:

> drwxr-xr-x@ 5 Vlad staff 170 14 Jan 21:17 .
>>>> drwxr-xr-x@ 17 Vlad staff 578 15 Jan 06:44 ..
>>>> -rwxr-xr-x@ 1 Vlad staff 111 1 Nov 2016
>>>> Delete_Viewed_Video_in_Lynda_TOC.pl
>>>> -rwxr--r--@ 1 Vlad staff 69 15 Jan 06:32 Randomize.sh
>>>> -rwxr-xr-x@ 1 Vlad staff 80 4 Nov 2015
>>>> Selection_To_Numbered_List.sh
>>>>
>>>> How is this possible?
>
>>>
>>> See the `x` in column 4? That indicates that `Randomize.sh` is in
>>> fact executable, _by the owner_ (you). BBEdit also runs as you, so
>>> it can execute the script just fine.
>
>>
>> Why do the others have an x on column 7 (that's groups, right?!)?
>
>
> Because the group (staff) has read and execute (search for
> directories) access on them.

Sorry, wrong question!

How come that the others have an x on column 7 and Randomize.sh not?
They were all created and stored in BBEdit!

Vlad Ghitulescu

unread,
Jan 16, 2018, 8:05:53 AM1/16/18
to bbe...@googlegroups.com

On 16 Jan 2018, at 14:02, Vlad Ghitulescu wrote:

They were all created and stored in BBEdit!

That's created and saved

Vlad Ghitulescu

unread,
Jan 16, 2018, 8:06:25 AM1/16/18
to bbe...@googlegroups.com

Hi, Jean-Christophe!

On 15 Jan 2018, at 13:58, Jean-Christophe Helary wrote:

On Jan 15, 2018, at 14:46, Vlad Ghitulescu <Vl...@Ghitulescu.de> wrote:

What are the GNU Coreutils good for?

They are common Linux utilities of which some BSD equivalents are present on macOS.

https://www.gnu.org/software/coreutils/manual/coreutils.html

Oh, there's a lot!

Should I install them too? (I've read about the difference in the performance of the various randomizer solutions in the previous cited StackOverflow-threads)
And if so, how?

homebrew

https://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities

Aha!
Never thought I need this until now…

That means that the "stock macOS" on my Sierra 10.12.6 are older than… what?

:) High Sierra ?

:-D
You're right, didn't even came to mind due to

a) iMac late 2012 with
b) FusionDrive and
c) learning about the disadvantages of APFS for non-SSDs.

I never used sort -R before, but as I wrote the manual has 2015 as a date and Sierra was released in 2016, so I guess Apple saw fit to upgrade sort for High Sierra.

Would you mind checking the date at the end of the sort man page on your machine?

OH!

That's pretty old:


SEE ALSO
The full documentation for sort is maintained as a Texinfo manual. If the info and sort programs are properly installed at your site, the command

          info sort

   should give you access to the complete manual.

sort 5.93 November 2005 SORT(1)


Ok, I'll wait for the next one and the reconsider homebrew! :-)

Thanks!

Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com

Regards,
Vlad

Jean-Christophe Helary

unread,
Jan 16, 2018, 8:17:26 AM1/16/18
to bbe...@googlegroups.com


> On Jan 16, 2018, at 21:34, Vlad Ghitulescu <Vl...@Ghitulescu.de> wrote:

> homebrew
>
> https://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities
>
> Aha!
> Never thought I need this until now…

Homebrew is interesting for the breadth of choice and for the numerous applications that have newer versions that will *never* be updated on macOS (the applications stuck in the past ended up that way seemingly because Apple wants to avoid GPL 3 licences.)

Also, besides for the command line tools, you also have access to full .app applications that are also available as brew packages.

> sort 5.93 November 2005 SORT(1)

That's pretty old...
Reply all
Reply to author
Forward
0 new messages