I have a file with 10K records and I need to divide it into 8 files. I used split -n l/8 -d file1 file2. This will split the file into 8 files, but I need to divide the file into 8 depending on the data/records in it.

For example,

       case $1
        "P"|"D") no_lines=$(wc -l $NAS_LAND/$infile | cut -d ' ' -f1 )

                #no of lines in the each file after split
                split_count=`expr $no_lines / 10`

               if [ $split_count -ge 1 ]
                    #Input file has more than or equal to 8 lines
                    echo "lines_per_instance=$split_count"
                    split  -n l/8 -d $NAS_LAND/$infile $MY_WORK/CCN_split_files/$infile 
                    cp $NAS_LAND/$infile $MY_WORK/CCN_split_files/ccn.email.list.file00

In the above code I am trying to count total number of records from In-file and doing a division split_count=expr $no_lines / 10 if it is greater than 1, split the file else do not split.

Now if my In-file contains 1000 records then file split into 8 files with 125 records each. if I change split_count=expr $no_lines / 10 to split_count=expr $no_lines / 100, it will split into 8 files with the same 125 records in each file. I am expecting when I changed the number to 100 (say1000/10=100) i am expecting 100 records in each of 8 files. where did I went wrong. The In-file must divide into 8 files, but records in the 8 files must vary by Dividor.

  • Do you mean that you always want 8 output files, no matter how many lines were in the original? What is wrong with the command you have? – terdon Mar 2 at 16:46
  • It is difficult to understand what you mean by 'But when File1 split records in 8 file should be different from File2 split records'. Please try with a better explanation. – sudodus Mar 2 at 19:09
  • What is a "record"? One line, several? Do you want to split each file separately or the union of both? – vonbrand Mar 3 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.