Randomized shuffle words on each line

Hi Folks,

I have a text file with a thousand lines consisting of words or a group of words separated by commas.

I would like to randomize / shuffle the words on each line.

Eg; file.txt

Linux,Open,Free,Awesome,Best Things in Life,The Greatest
Laptop,PC,Tablet,Home Computers,Digital
Bash,Tutorial,Shell,Terminal,Code,Command Line

Desired output ( randomized any order ):

Best Things in Life,Awesome,The Greatest,Free,Linux,Open
PC,Home Computers,Tablet,Digital,Laptop
Tutorial,Shell,Bash,Command Line,Code,Terminal

Any ideas on how to accomplish this would be greatly appreciated. Thank you for your help :b:

Maybe something like this:

#!/usr/bin/env perl
# shuffler.pl

use strict;
use warnings;

use List::Util qw(shuffle);

while(<>) {
    chomp;
    my @line = split ",";
    @line = shuffle @line;
    print join(",", @line), "\n";
}

Save as shuffler.pl
Run as perl shuffler.pl martinsmith.file

---------- Post updated at 07:52 PM ---------- Previous update was at 07:43 PM ----------

If you need it as a one-liner.

perl -MList::Util=shuffle -nlaF"," -e 'print join ",", (shuffle @F)' martinsmith.file
1 Like

Wow totally awesome! It does exactly what i needed.

Thanks so much for your help. Appreciated tremendously! :slight_smile: