Breaking a file into three new files, character by character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking a file into three new files, character by character
# 1  
Old 12-30-2009
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).

Code:
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!

Last edited by joeyg; 12-30-2009 at 05:08 PM.. Reason: Added codetags for easier viewing
# 2  
Old 12-30-2009
how do you decide on that random distribution of the character??? there should be some logic right??
# 3  
Old 12-30-2009
It should be random, and not systematic. The character should have a fairly equal chance of being assigned to File 1, 2, or 3.
# 4  
Old 12-30-2009
Your logic needs a review. There should be multiple spaces
Code:
ABCDEFGHIJK     main file
A   D        J       file1
  B     F   I         file2
    C  E GH  K     file3

bash does this with
Code:
${string:position:length}

example:
Code:
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
# 5  
Old 12-30-2009
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,
Code:
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.
# 6  
Old 12-30-2009
this will print the character in random way..
Code:
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...
# 7  
Old 12-30-2009
Debian

Code:
$ 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:
Code:
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=""
}


Last edited by alister; 12-30-2009 at 08:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

4. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

5. Shell Programming and Scripting

How to read character by character in a file

Hi, How read character by character from a file . and i need replace '.' with null if it comes as a 5 character i am beginner ...please help me (1 Reply)
Discussion started by: kartheek
1 Replies

6. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

7. Shell Programming and Scripting

Can I read a file character by character?

Hello all respected people, Can i read a file character by character without using sed,awk and perl commands. Thanks in advance. (4 Replies)
Discussion started by: murtaza
4 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

10. Shell Programming and Scripting

Can i read a file character by character

How to read character by character using awk (6 Replies)
Discussion started by: karnan
6 Replies
Login or Register to Ask a Question