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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Making replicates of a file with part of a line randomized for each file
# 1  
Old 08-10-2011
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:

Quote:
Individual_1A
Individual_2A
Individual_3A
Individual_4A
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:

Quote:
Individual_1B
Individual_2A
Individual_3A
Individual_4B
and

Quote:
Individual_1A
Individual_2A
Individual_3B
Individual_4A
etc.

I know the end product I want, but I have absolutely no idea how to go about this or even what kinds of commands I would use.

Thanks.
# 2  
Old 08-10-2011
Java

Assuming that you can use perl, here is a simple script that does what you are looking for:
Code:
#!/usr/bin/perl

@a = <>;

$i=1;
$j=0;

while ($i < 101) {
    open(my $fh, ">", "./file$i.out") || die "ERROR: Failed to open file ./file$i.out";

    select($fh);

    while ($j < $#a + 1) {   
        $c=chr(65 + int(rand(2)));

        print substr($a[$j], 0, -2) . $c . "\n"; 

        $j++;   
    }   

    close($fh);
    $i++;
    $j = 0;
}

Copy these lines into a file named, say, randomize.pl.

Run it as follows (where file1 is the name of your original file):
Quote:
randomize.pl file1
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

4. UNIX for Dummies Questions & Answers

Grep a part of a line from a file

Hi, I have a file with thousands of lines as below INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
Discussion started by: sudhakar T
5 Replies

5. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

6. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

7. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

8. Shell Programming and Scripting

Change in last line of each part of a file

Hi, I have a input files as below. Sample of one input file type mtg_key = record decimal("\344") id; string("\344") ct_cd; string("\344") st_cd; end; type psl_key = record decimal("\344") id; utf8 string("\344") st_cd; end; type amk_fields = record ... (6 Replies)
Discussion started by: adgangwar
6 Replies

9. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

10. Shell Programming and Scripting

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like: the errors I'm looking for with other errors: Alerts Password Incorrect Login Error Another Error Another Error 2 Other Info or, just the errors I need to parse for: Alerts Password Incorrect ... (9 Replies)
Discussion started by: elinenbe
9 Replies
Login or Register to Ask a Question