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
# 8  
Old 12-31-2009
ksh/bash version:
Code:
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

# 9  
Old 12-31-2009
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...
# 10  
Old 12-31-2009
Quote:
Originally Posted by foxcastle
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
# 11  
Old 12-31-2009
Quote:
Originally Posted by alister
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.
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