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!
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.
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=""
}
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...
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.