Splitting large files

Hi Unix gurus,

We have a masterfile which is to be split into smallerfiles with names as
masterfile00,masterfile01,masterfile03...etal
I was able to split the file using the "Split" cmd
but as masterfileaa,masterfileab..
Is it posiible to change the default suffix?
or is there any other cmd which does the needful
or do we need to rename the smallerfiles

thanking you in anticipation
Rvbs

hi Rvbs,

i dont think so if its possible to change the format of "split" command as per your requiremnet, so i guess you have to write a shell script to modify the ouput of "split" command.

i have written a sample shell script which will split the files into your desired format (suffix as numerals).

please test the same :stuck_out_tongue: ....n tell me if it works fine :slight_smile: ...

note:

  1. you have to pass the name of file to split and the number of lines for splitting the input files, as the command line argumnets ($1 and $2 in the same order as above)
  2. run the script from same directory/shell where your input file is existing or else give the proper path :slight_smile:
  3. you can modify the split command used here for any different options/flags...as per your requirement.

script:

set -x
#/usr/bin/sh
export name_of_file_to_split=$1
export num_of_lines=$2
export PWD=`pwd`
export len_of_input_file_name=`echo $name_of_file_to_split | wc -c`
export len_of_input_file_name=`expr $len_of_input_file_name - 1`
split -l "$num_of_lines" $name_of_file_to_split $name_of_file_to_split
ls $name_of_file_to_split* > temp_file
export input_file=$PWD/temp_file
number_of_sub_files=`cat $input_file | wc -l`
i=2
while [ $i -le $number_of_sub_files ]
do
old_file_name=`cat $input_file | head -"$i" | tail -1`
len=`echo $old_file_name | wc -c`
len=`expr $len - 1`
len_to_cut=`expr $len_of_input_file_name + 1`
old_suffix=`echo $old_file_name | cut -c "$len_to_cut"-"$len"`
new_suffix=`echo $old_suffix | tr a-z 0-9`
mv $old_file_name "$name_of_file_to_split""$new_suffix"
i=`expr $i + 1`
done
rm -f temp_file

regards,
Bhups

csplit should do the job, try 'man csplit'