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
# 1  
Old 05-05-2010
Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow.

INPUT: Input file name A
1
2
3
4
5
6
7
8
9
........100

Output: 4 output files (x,y,z,w)

File x should contains (Skip 4 lines)
1
5
9
13
17
21
............

File y should contains (skip 4 lines)
2
6
10
14
18
22
...........

File z should contains (skip 4 lines)
3
7
11
15
19
23
...................

File w should contain (skip 4 lines)
4
8
12
16
20
24

Regards
Subba
# 2  
Old 05-05-2010
What have you tried?
# 3  
Old 05-05-2010
Code:
sed -n '1,${p;n;n;n;}' A > x
sed -n '2,${p;n;n;n;}' A > y
sed -n '3,${p;n;n;n;}' A > z
sed -n '4,${p;n;n;n;}' A > w

# 4  
Old 05-05-2010
Code:
sed -n 'w x
     n; w y
     n; w z
     n; w w' A

# 5  
Old 05-05-2010
Hi
Awk answer for this one will be ;


Code:
awk 'BEGIN{a[1]="x";a[2]="y";a[3]="z";a[0]="w";} {var=NR%4;file=a[var];print > file;}' A


Thanks
Guru.
# 6  
Old 05-06-2010
Code:
awk '{print >>("file"(NR%4))}' file


Last edited by danmero; 05-06-2010 at 08:04 AM.. Reason: Fix typo ;)
# 7  
Old 05-06-2010
try
Code:
sed -n -e '1~4w x' -e '2~4w y' -e '3~4w z' -e '4~4w a' Your_file_name

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