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

Thanks & Regards,
Kumar66

Afaik, there is no such option for split, as stated in the man page. Split on Debian for example offers a -d for using numbers instead of letters, but it will be always a suffix at the end and not be positioned somewhere inside/between a string.
You might have to accept it as it is or could move the splitted files to the desired names.

hi ,

Thanks for your reply. Is there any way to split the files ? Or by writing a script?

As I am new to Unix , I am not familar with scripts .

Please Advise.

Thanks & Regrads,
Kumar.

Uhm you used split already to split files... :confused:
Yes, you can achieve something like that with awk head etc. for example.

You could use split, since it does a good job.
Since split makes the files alphabetically, you could change the filenames afterwards, like so (leave out the "echo" if you want to execute the mv, this is just to show what it would do).

# j=0; for i in `ls -1 ABC.txt??`; do j=`expr $j + 1`; echo mv $i ABC$j.txt; done
mv ABC.txtaa ABC1.txt
mv ABC.txtab ABC2.txt
mv ABC.txtac ABC3.txt
mv ABC.txtad ABC4.txt
mv ABC.txtae ABC5.txt
mv ABC.txtaf ABC6.txt
mv ABC.txtag ABC7.txt
mv ABC.txtah ABC8.txt
mv ABC.txtai ABC9.txt

can you try with "csplit"?

Hay the below example will do the following

  1. will split the file into pieces of 5 lines per file(i specified 6, it will take 6-1=5)
  2. -k option will Leaves previously created files intact, if any error occurs.
  3. -f option for provide the prefix of the file
  4. line_above_it -> is my source file.
csplit -k -f ABC line_above_it 6 {10}

Why don't you simply rename the files afterwards? You know the files name already ("some_name"aa, "some_name"ab, etc.). "ls" sorts them alphabetically already, yes? Now set up a counter, cycle through the filenames sorted this way and rename them using the filename template and the counter:

(( iCnt = 1 ))
ls | while read filename ; do
     mv $filename file${iCnt}.txt
     (( iCnt += 1 ))
done

I hope this helps.

bakunin

Hi All,

Thanks a ton.

#!/bin/ksh
split -l 100 FilePO1.txt 'FilePO1.txt'
j=0
for i in `ls -1 File.txt??`;
do j=`expr $j + 1`;
mv $i File$j.txt;
done

I used this script and it worked for me.

With Regards,
Kumar66