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

Hello Friends,

Can anyone help me for the below requirement.

I am having a file called Input.txt.
My requirement is first check the count that is wc -l input.txt
If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split the file in 10 number of subfiles.

Can anyone help me how can I do this?

Thanks in advance

Hi malaya kumar,

So if you have an input file with 22 lines, output should be three files: 1.txt with ten lines, 2.txt with ten lines and 3.txt with 2 lines. Is like that?

how about:

#!/bin/bash
lines=`wc -l doc.txt|awk '{ print $1 }'`
if [ $lines -ge 10 ];
then
split -d -l 10 doc.txt wcount.
fi
exit

This should suffice:

split -dl10 input.txt input.txt

@Birei:

My requirement is I am having a file input.txt which is having 7 records i.e.

11
12
13
14
15
16
17

Now I need to first check how many rows in that file.
After that if the number of rows in the file is less than 10 then I don't want to split it.
However if the number of rows is equals to greater than 10 then I need to split the input.txt file to 10 subfiles.

---------- Post updated at 04:13 AM ---------- Previous update was at 03:59 AM ----------

Hello,

Certainly this command is not working for me....
Can you please check it one more time

In what way, exactly? Do you get an error? An unexpected result? Your system explodes?

@CarloM,

Dude system didnt explodes.

I told that split -dl10 input.txt input.txt didnt work.
I am getting the error as :
split: Not a recognized flag: d
Usage: split [-l Line_Count] [-a Suffix_Length] [File [Prefix]]
or: split -b Number[k|m] [-a Suffix_Length] [File [Prefix]]

Appriciate your help guys.

Try

split -l10 input.txt input.txt

(-d is a GNU split flag which uses numeric suffixes rather than alphabetic)

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.

#! /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

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.

Modifying maskofzorro's solution a little:

#!/bin/bash

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

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.

Change the split command to:

split -a1 -l $newlines input.txt input.txt