dnat
June 24, 2009, 2:39am
1
Hi All,
I have a requirement .I want to split a file and the split files should have certain names.
Currently when i use the split command
split -1000 testdata testdata_
Then the output is
testdata_aa
testdata_bb
testdata_cc
and so on.
But i want the output as
testdata1.snd
testdata2.snd
testdata3.snd
etc..can we achieve this..Since the number of output files are dynamic, i am not sure how to do this.
my hints for your issue
sort the generated files rename them
ls -1 testdata_* | sort -t "_" -n -k2,2 -k3,3 -k4,4
Then from the sort list you can rename in a for or while loop
use the below code:
k=1
for i in $( ls test* )
do
mv -f $i ${i%%_*}$k.snd
((k++))
done
Best Regards
nawk -v f="$1" -v l="${2-1}" 'BEGIN{n=1}
{
file=sprintf("%s%s.snd",f,n)
print $0 >> file
if(NR % l ==0)
{
file=sprintf("%s%s.snd",f,n)
n++
}
}' yourfile
perl:
my $n=1;
my ($file,$line)=@ARGV;
$line||=1;
open FH,"<$file";
my $outfile=sprintf("%s%s.snd",$file,$n);
open OFH,">>$outfile";
while(<FH>){
print OFH $_;
if ($. % $line eq '0'){
$n++;
$outfile=sprintf("%s%s.snd",$file,$n);
open OFH,">>$outfile";
}
}