0

I have a text file which contains famous quotations.

I want to write a bash shell script that does the following:

  1. reads all the lines of the file into an array (each line is an array entry)
  2. 'Shuffles' or randomizes the array positions
  3. Loops over the 'randomized' array and prints the current line

I am new to bash scripting. Can anyone show me how to do this?

I am running on Linux Ubuntu.

This is what I have at the moment:

while read -r -a array
do
  print "${array[@]}"
done < myfile.txt

I need to randomize the read lines. Anyone knows how I can modify the script to do what I want?

I tried using sort --random-sort like this:

done < cat myfile.txt | sort --random-sort

But bash issued error messages

  • 1
    @th3dude, please don't tempt people into reposting the very same question again. Just vote, that's all -- thanks! @Takashi, your question will be moved t Stack Overflow automatically, *if applicable*. No need to post the same question again! – Arjan Dec 15 '10 at 23:17
  • [Answered on StackOverflow here.](http://stackoverflow.com/questions/2153882/how-can-i-shuffle-the-lines-of-a-text-file-in-unix-command-line) – Pete Ashdown Dec 15 '10 at 19:57

1 Answers1

0

You can use the fortune program. You may have to modify your file to include the required delimiters.

To fix the error message:

done < <(sort --random-sort myfile.txt)

or

sort --random-sort myfile.txt | while
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439