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

2
  • Answered on StackOverflow here. Commented Dec 15, 2010 at 19:57
  • 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! Commented Dec 15, 2010 at 23:17

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

Comments