I have read this post Select random lines from a file in bash and Random selection of columns using linux command however they don't work specifically with a set of lines that need to stay in the same order. I also searched to find if there was any randomization option using the cut
command.
My attempt:
I am trying to replace spaces with new lines, then sort Randomly and then use Head to grab a random string (for each line).
cat file1.txt | while read line; do echo $line | sed 's/ /\n/g' | sort -R | head -1
While this does get the basic job done for one random string, I would like to know if there is a better more efficient way of writing this code? This way, I can add the options to get 1-2 random strings rather than just one.
Here's file1.txt:
#Sample #Example #StackOverflow #Question
#Easy #Simple #Code #Examples #Help
#Support #Really #Helps #Everyone #Learn
Here's my desired output (random values):
#Question
#Code #Examples
#Helps
If you know a better way to implement this code, I would really appreciate your positive input and support.