Bash one liner to randomize lines in file

eshroom

macrumors 6502
Original poster
Oct 18, 2006
283
1
I got it from this website and it goes something like this:

for i in `cat unusual.txt`; do echo “$RANDOM $i”; done | sort | sed -r ’s/^[0-9]+ //’ > randorder.txt

This is for Linux, but no matter how I alter it I can't get it to work in terminal... Any ideas?

I changed -r to -e and get error message: "sed: 1: "’s/^[0-9]+\n": invalid command code ?"

Thanks
 

eshroom

macrumors 6502
Original poster
Oct 18, 2006
283
1
SOLVED

Thank you to SirDice at MacOSXHints, the answer is:

for i in `cat unusual.txt`; do echo "$RANDOM $i"; done | sort | sed -E 's/^[0-9]+ //' > randorder.txt

I should have capitalised the -e
 

sean_d

macrumors newbie
Jan 11, 2018
1
0
Note: The above splits the input file by all whitespace, not just line breaks (so it only works if there's only one word per line).

Here's a modified version that works on full lines instead:

while read i; do echo "$RANDOM $i"; done < unusual.txt | sort | sed -E 's/^[0-9]+ //' > randorder.txt