Breaking a file into three new files, character by character

I am new to shell scripting, and need a script to randomly distribute each character from a file into one of three new files. I also need each character to maintain it's position from the original file in the new file (such that if a character is written to File 1, Files 2 and 3 have spaces written to them).

For example, if 
File 0 should be  "ABCDEFGHIJK": 
File 1 should be, "A  DE    J "
File 2 should be, " B   F  I  "
File 3 should be, "  C   GH  K"

I'm not sure how to read a file character by character in order to accomplish this. Any advice would be greatly appreciated. Thanks!

how do you decide on that random distribution of the character??? there should be some logic right??

It should be random, and not systematic. The character should have a fairly equal chance of being assigned to File 1, 2, or 3.

Your logic needs a review. There should be multiple spaces

ABCDEFGHIJK     main file
A   D        J       file1
  B     F   I         file2
    C  E GH  K     file3

bash does this with

${string:position:length}

example:

var="Hi there"
char3=${var:2:1}
echo $char3

var:2:1 means the variable "var", position 3 (start counting at zero, not one), 1 character

There should not be multiple spaces: as each character is written to whichever new file it randomly goes to, it should retain its position from the original file.

So,

1234567890 is the position, and
ABCDEFGHIJ is the original file

then A should always be in position 1 and F should always be in position 6 no matter if it's in File 1, 2, or 3; and in the two files that do not contain A, position 1 should be filled with a space.

this will print the character in random way..

awk 'function char_pos(n) { return 1 + int(rand() * n)}
{var=1;while(var<=length($0)){ch_pos=char_pos(2);printf "%s\n",substr($0,var,ch_pos);var+=ch_pos}}' file0

try to print it on different files as you want...

$ cat r.awk 
BEGIN {FS=""; srand()}
{ for (i=1; i<=NF; i++) {
    r=int(n*rand())+1
    for (f=1; f<=n; f++)
      s[f]=s[f] (f==r ? $i : " ")
  }
  for (f=1; f<=n; f++)
    print s[f] >> ("file"f)
  delete s
}

$ cat data
1234567890
1234567890
1234567890
1234567890
1234567890

$ awk -f r.awk n=3 data 

$ cat file1
  3       
12     8  
 2 4 6    
12 4  78  
1234 6 89 

$ cat file2
12 45 78  
  3     90
  3    8  
    56  9 
    5 7  0

$ cat file3 
     6  90
   4567   
1   5 7 90
  3      0

I changed the code of this post after vidyadhar85 nominated it. Apologies if that's frowned upon, but i thought a parameterized version would be even better.

The original code was:

function chk_char(f,c) { if (f!=r) c=" "; return c} 
BEGIN {FS=""; srand()}
{ for (i=1; i<=NF; i++) {
    r=int(3*rand())+1
    s1=s1 chk_char(1,$i)
    s2=s2 chk_char(2,$i)
    s3=s3 chk_char(3,$i)
  }
  print s1 >> "file1"
  print s2 >> "file2"
  print s3 >> "file3"
  s1=s2=s3=""
}

ksh/bash version:

nr=3                                 # nr of output files
CHARS=$(<infile)                     # read characters
for (( n=0; n<${#CHARS}; n++ )) do   # for each character
  to=$(( RANDOM%nr+1 ))              # determine to which file
  for (( i=1; i<=nr; i++ )); do      # for each outfile
    if (( to == i )); then           # if the char goes here
      printf ${CHARS:n:1}            # write the character
    else                             #
      printf " "                     # write a space if no char
    fi >> outfile$i                  # append character to file
  done                               #
done                                 #
echo | tee -a outfile? >/dev/null    # write \n to every outfile

That is very helpful, and far more clever than anything I was about to come up with. Thanks, all!

---------- Post updated at 10:58 AM ---------- Previous update was at 10:11 AM ----------

Alister,

When I run the newer version of your code, awk tells me that there is a syntax error/illegal statement near lines 1, 3, and 5. I'm not sure what the error is...

Hi, foxcastle

I tested the code in gawk (using Cygwin on a Windows box) and nawk on an OSX machine. Neither version complained and both gave good results. Perhaps you're experiencing a copy-paste issue?

If it's not a simple copy-paste problem, perhaps specifics on the error messages and which version of AWK you're using may help shed some light on the malfunction.

Regards,
alister

Alister,

Ah, yep, gawk will do it. Thanks.