Command to split the files based on the number of lines in it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command to split the files based on the number of lines in it
# 8  
Old 03-29-2012
Try
Code:
split -l10 input.txt input.txt

(-d is a GNU split flag which uses numeric suffixes rather than alphabetic)
# 9  
Old 03-29-2012
Ok, that means your version of split doesn't support option 'd'.

Try this bash script. This would split the file into multiple files each containing 10 lines.

Code:
#! /bin/bash

c=1; f_num=1;
while read x
do
    [ $c -eq 11 ] && c=1 && ((f_num++))
    echo $x >> input.txt.$f_num
    ((c++))
done < input.txt

# 10  
Old 03-29-2012
Hello CarloM,

Thank you very much for your quick response.
Certainly this command is working, but its not fullfiling my requirement.

Let me tell you what exactly I want:

If Input.txt is having 9 rows that is

1
2
3
4
5
6
7
8
9

Then I don't want to split it.

If Input file is having 10 or more than 10 records then I want to split the input file into 10 subfiles.

Hope you understood my requirement.

---------- Post updated at 04:38 AM ---------- Previous update was at 04:34 AM ----------

Hello Balajesuri,

Thank you very much for your reply.But this is not fullfiling my requirement.

Please see my requirement once again:

I am having one input file i.e. Input.txt
It has 9 rows.Sometime it will have more than 9 rows also.

So what I want is I want to check if the total number of records in the input.txt is equals to 9 or less than 9 then DON'T SPLIT. However if total number of records in Input.txt is equals to greater than 10 then SPLIT THE FILE IN 10 NUMBER OF SUBFILES.

Thank you very much in advance.

---------- Post updated at 04:39 AM ---------- Previous update was at 04:38 AM ----------

Hello Balajesuri,

Thank you very much for your reply.But this is not fullfiling my requirement.

Please see my requirement once again:

I am having one input file i.e. Input.txt
It has 9 rows.Sometime it will have more than 9 rows also.

So what I want is I want to check if the total number of records in the input.txt is equals to 9 or less than 9 then DON'T SPLIT. However if total number of records in Input.txt is equals to greater than 10 then SPLIT THE FILE IN 10 NUMBER OF SUBFILES.

Thank you very much in advance.
# 11  
Old 03-29-2012
Modifying maskofzorro's solution a little:
Code:
#!/bin/bash

lines=`wc -l input.txt|awk '{ print $1 }'`
if [ $lines -ge 10 ];
then
   (( newlines=$lines/10 ))
   split -l $newlines input.txt
fi

# 12  
Old 03-29-2012
Hello CarolM,

Thank you very much for your help.

It worked fine for me.

However after running this script the file I am getting is something like:

xaa
xab

So I njust need the naming of the outfile is Input.txt1 or Input.txta

Kindly reply on this.
# 13  
Old 03-29-2012
Change the split command to:
Code:
split -a1 -l $newlines input.txt input.txt

 
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 File based on number of rows

Hi I have a requirement, where i will receive multiple files in a folder (say: /fol1/fol2/). There will be at least 14 to 16 files. The size of the files will different, some may be 80GB or 90GB, some may be less than 5 GB (and the size of the files are very unpredictable). But the names of the... (10 Replies)
Discussion started by: kpk_ds
10 Replies

3. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

4. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

5. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

6. Shell Programming and Scripting

split a csv file into specified number of files (not lines)

hi, i really need it ...it's not simple to explain but as it's part of a crontab i can't split the file manually...and the file can change every day so the lines are not a good base. example: how to split 1 csv file in 15 files? thank you very much regards :b: (4 Replies)
Discussion started by: 7stars
4 Replies

7. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

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

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. Shell Programming and Scripting

split based on the number of characters

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT... (6 Replies)
Discussion started by: chriss_58
6 Replies
Login or Register to Ask a Question