Split a file into multiple files with an extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a file into multiple files with an extension
# 1  
Old 04-05-2013
Split a file into multiple files with an extension

Hi

I have a file with 100 million rows. I want to split them into 1000 subfiles and name them from 1.xls to 1000.xls.. Can I do it in awk?

Thanks,
# 2  
Old 04-05-2013
Do a man on split command but if you wish to use awk, try this (untested):

Code:
 
awk 'NR%100000==1 {filenum=++i;} {print > filenum".xls"}' input.file


Last edited by mjf; 04-05-2013 at 10:15 PM.. Reason: changed 1000 to 100000
# 3  
Old 04-05-2013
Hi,

Thanks, It does not work for a file with lines 37086834...

It just simply returns nothing..
# 4  
Old 04-05-2013
If your .xls files are excel spreadsheets, awk and split are not going to work. The awk utility is designed to work on text files. Microsoft's excel spreadsheets are binary files; not text files. The cut utility will work on binary files, but there is no reason to believe that you could use cut -l since there is no reason to believe that rows in a table in a .xls file have a 1-to-1 correspondence to newlines in the .xls file.

If you export the table from excel into a .csv text file, then awk and cut and lots of other utilities could be used to split the .csv file into smaller .csv files containing a set number of lines desired in each resulting file.
# 5  
Old 04-06-2013
Yes, you can do it. Use 100000 instead of 10 in the awk command below:
Code:
$ seq 100 > file

$ awk 'BEGIN { f = 1 } i == 10 { f++; i = 0 } { print >> f ".xls"; i++ }' file

$ ls
1.xls   2.xls  4.xls  6.xls  8.xls  file
10.xls  3.xls  5.xls  7.xls  9.xls

$ cat 6.xls
51
52
53
54
55
56
57
58
59
60


Last edited by hanson44; 04-06-2013 at 12:21 AM.. Reason: play it safe with >>
# 6  
Old 04-06-2013
Quote:
Originally Posted by hanson44
Yes, you can do it. Use 100000 instead of 10 in the awk command below:
Code:
$ seq 100 > file

$ awk 'BEGIN { f = 1 } i == 10 { f++; i = 0 } { print >> f ".xls"; i++ }' file

$ ls
1.xls   2.xls  4.xls  6.xls  8.xls  file
10.xls  3.xls  5.xls  7.xls  9.xls

$ cat 6.xls
51
52
53
54
55
56
57
58
59
60

hanson44,
Note that your file is a text file. It is not an excel spreadsheet!

Create a spreadsheet using excel with the values 1 to 100 in a table in the spreadsheet and then show me that this awk script will produce 10 output files with the values 51 through 60 in the 6th output file when given the .xls file produced by excel as its input.
# 7  
Old 04-06-2013
I never suggested the script would work on an excel spreadsheet. The script is designed to operate on a text file, as I made clear by providing an example.

The poster just said "file". You're right to assume an xls file "should be" a spreadsheet. But maybe the poster gives text files an xls extension for some odd reason of their choosing. They will have to clarify.

If it is a spreadsheet (100 million rows? Smilie), the poster could of course convert it to a csv or tsv file first, and change the xls extension in the script to "csv" or "tsv".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying multiple files and appending time stamp before file extension

Hi, I have multiple files that read: Asa.txt Bad.txt Gnu.txt And I want to rename them using awk to Asa_ddmmyytt.txt and so on ... If there is a single command or more efficient executable please share! Thanks! (4 Replies)
Discussion started by: Jesshelle David
4 Replies

2. Shell Programming and Scripting

Split a .csv File into Multiple Files

Hi guys, I have a requirement where i need to split a .csv file into multiple files. Say for example i have data.csv file and i have splitted that into multiple files based on some conditions i.e first file should have 100, last file 50 and other files 1000 each. Am passing the values in... (2 Replies)
Discussion started by: azherkn3
2 Replies

3. Shell Programming and Scripting

Split file into multiple files using delimiter

Hi, I have a file which has many URLs delimited by space. Now i want them to move to separate files each one holding 10 URLs per file. http://3276.e-printphoto.co.uk/guardian http://abdera.apache.org/ http://abdera.apache.org/docs/api/index.html I have used the below code to arrange... (6 Replies)
Discussion started by: vel4ever
6 Replies

4. Shell Programming and Scripting

split file into multiple files

Hi, I have a file of the following syntax that has around 120K records that are tab separated. input.txt abc def klm 20 76 . + . klm_mango unix_00000001; abc def klm 83 84 . + . klm_mango unix_0000103; abc def klm 415 439 . + . klm_mango unix_00001043; I am looking for an awk oneliner... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Split file into multiple files

Hi I have a file that has multiple sequences; the sequence name is the line starting with '>'. It looks like below: infile.txt: >HE_ER tttggtgccttgactcggattgggggacctcccttgggagatcaatcccctgtcctcctgctctttgctc cgtgaaaaggatccacctatgacctctagtcctcagacccaccagcccaaggaacatctcaccaatttca >M7B_Ho_sap... (2 Replies)
Discussion started by: jdhahbi
2 Replies

6. Shell Programming and Scripting

Split a file into multiple files

Hi, i have a file like this: 1|2|3|4|5| 1|2|8|4|6| Trailer1||||| 1|2|3| Trailer2||| 3|4|5|6| 3|4|5|7| 3|4|5|8| Trailer2||| I want to generate 3 files out of this based on the trailer record. Trailer record string can be different for each file or it may be same for one or two. No... (24 Replies)
Discussion started by: pparthji
24 Replies

7. UNIX for Dummies Questions & Answers

Removing prefix from multiple files and renaming file extension

Hello i have the files in this format pdb1i0t.ent pdb1lv7.ent pdb1pp6.ent pdb1tj2.ent pdb1xg2.ent pdb2b4b.ent pdb2ewe.ent Now i have to remove the prefix pdb from all the files and also i need to change the extension of .ent to .txt The new file should look like this ... (3 Replies)
Discussion started by: empyrean
3 Replies

8. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies

9. UNIX for Dummies Questions & Answers

split a file into multiple files

Hi All, I have a file ABC.txt and I need to split this file on every 250 rows. And the file name should be ABC1.txt , ABC2.txt and so on. I tried with split command split -l 250 <filename> '<filename>' but the file name returned was ABC.txtaa ABC.txtab. Please... (8 Replies)
Discussion started by: kumar66
8 Replies

10. Shell Programming and Scripting

Split a file into multiple files

I have a file ehich has multiple create statements as create abc 123 one two create xyz 456 four five create nnn 666 six four I want to separte each create statement in seperate files (3 Replies)
Discussion started by: glamo_2312
3 Replies
Login or Register to Ask a Question