Randomized shuffle words on each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Randomized shuffle words on each line
# 1  
Old 12-08-2015
Question 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
Code:
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 ):
Code:
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 Smilie

Last edited by martinsmith; 12-08-2015 at 10:42 PM..
# 2  
Old 12-08-2015
Maybe something like this:

Code:
#!/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.
Code:
perl -MList::Util=shuffle -nlaF"," -e 'print join ",", (shuffle @F)' martinsmith.file

This User Gave Thanks to Aia For This Post:
# 3  
Old 12-09-2015
Quote:
Originally Posted by Aia
Maybe something like this:

Code:
#!/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.
Code:
perl -MList::Util=shuffle -nlaF"," -e 'print join ",", (shuffle @F)' martinsmith.file

Wow totally awesome! It does exactly what i needed.

Thanks so much for your help. Appreciated tremendously! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

3. UNIX for Dummies Questions & Answers

Making replicates of a file with part of a line randomized for each file

Ok, so let's say that I have a file like the following: I want to create 100 replicates of this file, except that for each file, I want different randomized combinations of either A or B at the end of each line so that I would end up with files like the following: and etc. I... (1 Reply)
Discussion started by: Scatterbrain26
1 Replies

4. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

5. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

6. Shell Programming and Scripting

shuffle pack of words in line

hello i just seeking for a simple way to make a shuffle by block of words in a line. no matter shell (sh/bash) or perl should be like this: the message (which is line of some file) splits to packs (packs are random 5-10 words in each) then making a new line inserting those packs in a random... (9 Replies)
Discussion started by: tip78
9 Replies

7. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies
Login or Register to Ask a Question