The Unix sort command is one of those handy tools that make it such a great environment for mucking about in.
A less-common need is to shuffle lines in a file. Here’s a one-liner that I found on the comp.unix.shell archives. Its performance seems to be reasonable for casual use, i.e. on the 51,000 line file I have, it took 1.3S.
$ cat unsort perl -wne 'printf "%016.0f%s", rand 2**53, $_' | sort | cut -b17- $
An example:
$ cat file.sorted 01 To be, or not to be: that is the question: 02 Whether 'tis nobler in the mind to suffer 03 The slings and arrows of outrageous fortune, 04 Or to take arms against a sea of troubles, 05 And by opposing end them? To die: to sleep; 06 No more; and by a sleep to say we end 07 The heart-ache and the thousand natural shocks 08 That flesh is heir to, 'tis a consummation 09 Devoutly to be wish'd. To die, to sleep; 10 To sleep: perchance to dream: ay, there's the rub; 11 For in that sleep of death what dreams may come 12 When we have shuffled off this mortal coil, 13 Must give us pause: there's the respect 14 That makes calamity of so long life; 15 For who would bear the whips and scorns of time, 16 The oppressor's wrong, the proud man's contumely, 17 The pangs of despised love, the law's delay, 18 The insolence of office and the spurns 19 That patient merit of the unworthy takes, 20 When he himself might his quietus make 21 With a bare bodkin? who would fardels bear, 22 To grunt and sweat under a weary life, 23 But that the dread of something after death, 24 The undiscover'd country from whose bourn 25 No traveller returns, puzzles the will 26 And makes us rather bear those ills we have 27 Than fly to others that we know not of? 28 Thus conscience does make cowards of us all; 29 And thus the native hue of resolution 30 Is sicklied o'er with the pale cast of thought, 31 And enterprises of great pith and moment 32 With this regard their currents turn awry, 33 And lose the name of action. - Soft you now! 34 The fair Ophelia! Nymph, in thy orisons 35 Be all my sins remember'd.
$ ./unsort < file.sorted > file.unsorted
$ cat file.unsorted 32 With this regard their currents turn awry, 18 The insolence of office and the spurns 34 The fair Ophelia! Nymph, in thy orisons 19 That patient merit of the unworthy takes, 14 That makes calamity of so long life; 04 Or to take arms against a sea of troubles, 10 To sleep: perchance to dream: ay, there's the rub; 06 No more; and by a sleep to say we end 24 The undiscover'd country from whose bourn 16 The oppressor's wrong, the proud man's contumely, 15 For who would bear the whips and scorns of time, 09 Devoutly to be wish'd. To die, to sleep; 17 The pangs of despised love, the law's delay, 30 Is sicklied o'er with the pale cast of thought, 29 And thus the native hue of resolution 12 When we have shuffled off this mortal coil, 31 And enterprises of great pith and moment 11 For in that sleep of death what dreams may come 03 The slings and arrows of outrageous fortune, 28 Thus conscience does make cowards of us all; 02 Whether 'tis nobler in the mind to suffer 27 Than fly to others that we know not of? 13 Must give us pause: there's the respect 07 The heart-ache and the thousand natural shocks 21 With a bare bodkin? who would fardels bear, 33 And lose the name of action. - Soft you now! 20 When he himself might his quietus make 05 And by opposing end them? To die: to sleep; 35 Be all my sins remember'd. 26 And makes us rather bear those ills we have 23 But that the dread of something after death, 01 To be, or not to be: that is the question: 25 No traveller returns, puzzles the will 22 To grunt and sweat under a weary life, 08 That flesh is heir to, 'tis a consummation