Split the single file lines into multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split the single file lines into multiple files
# 8  
Old 05-06-2010
Contrary to your nick, that tilde address step syntax is a GNU extension Smilie
(I mention it only so that if it fails for someone, they'll know why.)

Regards,
Alister
# 9  
Old 05-06-2010
a bash one

Code:
F=( x y z w )
for FILE in ${F[@]}; do :>$FILE; done # empty files if necessary
while read L
do    echo "$L" >> ${F[$((i%4))]}
       ((i++))
done < A

# 10  
Old 05-06-2010
Quote:
Originally Posted by frans
Code:
F=( x y z w )
for FILE in ${F[@]}; do :>$FILE; done # empty files if necessary
while read L
do    echo "$L" >> ${F[$((i%4))]}
       ((i++))
done < A

Nice solution, frans, but note that your read will strip leading and trailing whitespace, as well as discard backslashes and possibly merge lines. To preserve lines as they appear in the original file, you'd want to use:
Code:
while IFS= read -r L



---------- Post updated at 07:15 AM ---------- Previous update was at 07:11 AM ----------

A posix sh approach without arithmetic:
Code:
set x y z w
while IFS= read -r line; do
    echo "$line" >> $1
    set $@ $1
    shift
done < A

Regards,
Alister
# 11  
Old 05-06-2010
From Guru's solution :
Code:
awk -v Files="x,y,z,w" '
BEGIN { nbFiles = split(Files, file, /,/) ; file[0] = file[nbFiles] }
{ print > file[NR % nbFiles ] }
' A

Input file A:
Code:
1 x
2 y
3 z
4 w
5 x
6 y
7 z
8 w
9 x

Result files x, y, z and w:
Code:
$ cat x
1 x
5 x
9 x
$ cat y
2 y
6 y
$ cat z
3 z
7 z
$ cat w
4 w
8 w
$

Jean-Pierre.
# 12  
Old 05-06-2010
MySQL

Quote:
Originally Posted by alister

A posix sh approach without arithmetic:
Code:
set x y z w
while IFS= read -r line; do
    echo "$line" >> $1
    set $@ $1
    shift
done < A

Regards,
Alister
That's great !!
# 13  
Old 05-06-2010
Thanks for the response and your help.
Just one small change in my query, I would like to automate this like, I would like to specify the number of out put files through commond promt and I have tried the same but it did not work. it would be great if some one help me to make it work. Thanks in advance.

> set_script.sh 3 inputfile

#!/usr/bin/ksh
nof=$1
file=$2
for ((i=1; i<=nof; i++))
do
sed -n -e '$i~4w $x[i]' $file
done

Regards
Subba
# 14  
Old 05-07-2010
How do you want to name your files?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Split a single file into multiple files based on a value.

Hi All, I have the sales_data.csv file in the directory as below. SDDCCR; SOM ; MD6546474777 ;05-JAN-16 ABC ; KIRAN ; CB789 ;04-JAN-16 ABC ; RAMANA; KS566767477747 ;06-JAN-16 ABC ; KAMESH; A33535335 ;04-JAN-16 SDDCCR; DINESH; GD6674474747 ;08-JAN-16... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. UNIX for Dummies Questions & Answers

Split files into smaller ones with 1000 hierarchies in a single file.

input file: AD,00,--,---,---,---,---,---,---,--,--,--- AM,000,---,---,---,---,---,--- AR, ,---,--,---,--- AA,---,---,---,--- AT,--- AU,---,---,--- AS,---,--- AP,---,---,--- AI,--- AD,00,---,---,---, ,---,---,---,---,---,--- AM,000,---,---,--- AR,... (6 Replies)
Discussion started by: kcdg859
6 Replies

4. Shell Programming and Scripting

Single command to create multiple empty files(no trailing lines as well).

Hi, i need a single command to create multiple empty files(no trailing lines as well) and empty the files if already existing. please let me know or if this has been ansered, if some ocan share the link please, thanks > newfile.txt or :> newfile.txt do not work (4 Replies)
Discussion started by: Onkar Banerjee
4 Replies

5. Shell Programming and Scripting

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

6. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. UNIX for Dummies Questions & Answers

Split single file into n number of files

Hi, I am new to unix. we have a requirement here to split a single file into multiples files based on the number of people available for processing. So i tried my hand at writing some code as below. #!/bin/bash var1=`wc -l $filename` var2=$var1/$splitno split -l $var2 $1 Please help me... (6 Replies)
Discussion started by: quirkguy
6 Replies

9. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

10. UNIX for Dummies Questions & Answers

split a single sql file into multiple files

Hi,I have a single sql file containing many create table ddl's.Example: CREATE TABLE sec_afs ( rpt_per_typ_c char(1) NOT NULL, rpt_per_typ_t varchar(20) NULL, LOCK ALLPAGES go EXEC sp_primarykey 'sec_afs', rpt_per_typ_c go GRANT SELECT ON sec_afs TO developer_read_only... (5 Replies)
Discussion started by: smarter_aries
5 Replies
Login or Register to Ask a Question