> Trying to randomize entries in a fortune text file. Not the output--the text of the human-readable fortune file. "sort -R" works on individual lines, but I want to grab multiline entries delimited by %.
To which someone else replied
> I figured it would be a two step solution. Step 1: install perl.
Another person reacted with
> <3 #perl. If you'd use a Perl one-liner let me know, I'll see if I can get one working. :)
The hashtag in this reply was why I was informed about the discussion.
Here's a sample from a typical fortune file (from ruanyf/fortunes):
You cannot achieve the impossible without attempting the absurd. % Assumption is the mother of all screw-ups. % Thinking you know something is a sure way to blind yourself. % Neckties strangle clear thinking. % The first principle is that you must not fool yourself -- and you are +the easiest person to fool. -- Richard Feynman % The greater danger for most of us lies not in setting our aim too high + and falling short; but in setting our aim too low, and achieving our + mark. -- Michelangelo % I would rather have a mind opened by wonder than one closed by belief. -- Gerry Spence % What you are is what you have been. What you'll be is what you do now. -- Buddha % What we see is mainly what we look for. %
And here's my solution:
perl -0x25 -MList::Util=shuffle -e 'print +(shuffle<>)[0]'
Update: As noted by hippo, the original request was more probably just to randomly sort the whole file. To do that, just remove the [0].
Update 2: Unfortunately, there's the trailing empty cookie problem.
perl -0x25 -MList::Util=shuffle -e '@f=<>;pop@f;print shuffle@f'
In reply to A Random Fortune Cookie by choroba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |