Round Robin Distribution of Contents of file to 3 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Round Robin Distribution of Contents of file to 3 files
# 1  
Old 07-23-2009
Round Robin Distribution of Contents of file to 3 files

Hi
I need to create a script that distributes in round robin fashion the contents of a file to 3 files. The number of lines in a content of file can vary from 1-n(Each line is just a one letter word).The entire lines needs to get distributed into 3 files ( The order doesnt matter) , at the end of process the number of lines in each file should be more or less the same

eg
Input_file.txt
(Contents - 4 lines)
1
2
3
4

At the end of process

Op_1.txt
1
4

Op_2 .txt
2

Op_3.txt
3


Thanks for the help
# 2  
Old 07-23-2009
You can do something like that :
Code:
awk -v COUNT=3 '{ f=(f%COUNT)+1 ; print > "Op_" f ".txt" }' Input_file.txt

Jean-Pierre.
# 3  
Old 07-23-2009
Thanks

That was really helpful..Thanks
# 4  
Old 08-07-2009
Error Handling on AWK command

How can we do error handling on the awk commands.
Based on earlier threads i am doing this command.Accidently i discovered that if directory /tmp/temp/ is not there it throws out an error ( The directory names are parameterized, this is just an example) , but the return code is still equal zero

ls *.dat | awk -v COUNT=3 -v DIR="/tmp/temp/" '{ f=(f%COUNT)+1 ; print > DIR "_list_" f ".log" }'


Any suggestion ?
# 5  
Old 08-07-2009
Quote:
Originally Posted by police
How can we do error handling on the awk commands.
Based on earlier threads i am doing this command.Accidently i discovered that if directory /tmp/temp/ is not there it throws out an error ( The directory names are parameterized, this is just an example) , but the return code is still equal zero

ls *.dat | awk -v COUNT=3 -v DIR="/tmp/temp/" '{ f=(f%COUNT)+1 ; print > DIR "_list_" f ".log" }'


Any suggestion ?

awk needs an explicit declaration ( exit n ) of its return status ... so assuming you don't have an END statement one way would be:

Code:
$ ls *.dat | awk -v COUNT=3 -v DIR="/tmp/invalid_dir/" 'BEGIN{ if(system("test -d " DIR)>0) exit 2 }{ f=(f%COUNT)+1; print f }'
$ echo $?
2

$ ls *.dat | awk -v COUNT=3 -v DIR="/tmp/valid_dir/"   'BEGIN{ if(system("test -d " DIR)>0) exit 2 }{ f=(f%COUNT)+1; print f }'
1
2
3
1
2
3
$ echo $?
0


otherwise ( END present ):

Code:
ls *.dat | awk -v COUNT=3 -v DIR="/tmp/valid_dir" 'BEGIN{ if(system("test -d " DIR)>0) {e=1; exit 2}}{ f=(f%COUNT)+1; print f }
                                          END  { if(e) exit 2; else print "more actions here"}'

Or it might be easier to test for DIR existence beforehand at the shell level.
# 6  
Old 08-10-2009
Is there any generic way of handling all expection related to AWK .Like $? in Shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Need Script to copy the contents of two files into one file

Hi i need Script to copy the contents of two files into one file i have 2 fil X1.txt / X2.txt i need script to copy the contents of X1 and X2 In AllXfile X1.txt File X1 X2.txt File X2 AllXfile.txt File X1 File X2 (2 Replies)
Discussion started by: azzeddine2005
2 Replies

3. Programming

Round Robin Scheduling via UCONTEXT.H

Hi I am implementing Round Robin Scheduling using ucontext.h functions. Well i am using my own logic for round robin but i am stuck at one point. I am using swapcontext for shifting from one process to another. Now how do i get to know that after 4 sec(Round Robin Time) where the process has... (1 Reply)
Discussion started by: aditya08
1 Replies

4. UNIX for Dummies Questions & Answers

Round Robin Algorithm

Hey, guys I have a task: Job Running time Priority A 10 3 B 6 5 C 2 2 D 4 1 E 8 4 All 5 jobs have the same arrival time. The question is, what is the average waiting time according to Round Robin algorithm. Quantum = 1 min. The answer that was given by a... (1 Reply)
Discussion started by: Anne_Stark
1 Replies

5. Shell Programming and Scripting

How to mix the contents of 2 files into a new file?

Hello Everybody! My question is how can I mix for example file_a with file_b in the following method: After 2 lines of file_a put 2 lines from file_b to file_c. For example: file_a: 1 2 3 4 5 6 file_b: 11 22 (7 Replies)
Discussion started by: Levi85
7 Replies

6. UNIX for Dummies Questions & Answers

Move contents of a directory into one file for e-mail distribution ...

Hello, Here is what I am trying to accomplish. I am going to have one directory in which there will be files of varying types (Excel, Word, PPT, and possible others), and I need to be able to be bundle however many files there are in there together in to one file to be used as an e-mail... (3 Replies)
Discussion started by: rip73
3 Replies

7. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. Shell Programming and Scripting

writing files to a dir in round robin order

I have a list of directories. (say a1,a2,a3,a4,a5) I need to get the directory last modified and access the next one to put some files over there (say if a3 is the latest dir modified ie, last time files were put into a3, this time I need to move the files in a4)...and if a5 is the last modified... (6 Replies)
Discussion started by: kanchan_cp
6 Replies

9. Shell Programming and Scripting

comparing files to contents of a file

Hi I have a problem trying to run a while statement. I have files under one directory that i need to compare to a value in filex and update that file with the result files in the directory are DFC1. DFC5. DFC345. DFC344. DFC9. The program i am trying to run will take the number... (3 Replies)
Discussion started by: SummitElse
3 Replies

10. UNIX for Advanced & Expert Users

Round Robin Scheduling

Hi, first post. Well, here goes: Ok, so I need to build a round robin scheduling algorithm. I understand HOW the algorithm works and I can write it down/show you on paper if you were to ask me "how does the RR scheduling algorithm work?" Only problem is that I'm having a hell of a time... (0 Replies)
Discussion started by: ramoneguru
0 Replies
Login or Register to Ask a Question