Sponsored Content
Top Forums Shell Programming and Scripting Breaking a file into three new files, character by character Post 302383676 by alister on Wednesday 30th of December 2009 05:01:50 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
RAND(3) 						   BSD Library Functions Manual 						   RAND(3)

NAME
rand, rand_r, srand, sranddev -- bad random number generator LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdlib.h> int rand(void); int rand_r(unsigned *seed); void srand(unsigned seed); void sranddev(void); DESCRIPTION
These interfaces are obsoleted by random(3). The rand() function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file <stdlib.h>). The srand() function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the functions are automatically seeded with a value of 1. The sranddev() function initializes a seed, using the random(4) random number device which returns good random numbers, suitable for crypto- graphic use. The rand_r() function provides the same functionality as rand(). A pointer to the context value seed must be supplied by the caller. SEE ALSO
random(3), random(4) STANDARDS
The rand() and srand() functions conform to ISO/IEC 9899:1990 (``ISO C90''). The rand_r() function is as proposed in the POSIX.4a Draft #6 document. BSD
May 25, 1999 BSD
All times are GMT -4. The time now is 04:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy