
15.09.2007, 23:07
|
|
Banned
Регистрация: 12.01.2007
Сообщений: 113
Провел на форуме: 4094601
Репутация:
495
|
|
2 malamut
Код:
#!/usr/bin/perl -w
use strict;
use warnings;
use constant BACKUP_PREFIX => 'backup_';
use constant COMMENT => '\x2A';
use Getopt::Std;
my %options;
getopts( "f:" , \%options );
if( !exists $options{f} )
{
&usage;
}
else
{
my $m_file = $options{f};
&sort( $m_file );
}
sub sort
{
my $m_file = shift;
my $b_file = BACKUP_PREFIX."$m_file";
my $comment = COMMENT;
my @save = undef;
my @backup = undef;
open( my $file_m, "+<", $m_file ) || exit print "ERROR:Cant\'t open $m_file";
open( my $file_b, ">", $b_file ) ;
my $i = 0;
my $b = 0;
my $g = 0;
while( <$file_m> )
{
if( substr($_, 0, 1) =~/$comment/ )
{
$b++;
}
else
{
$save[$g] = $_;
$g++;
}
$backup[$i] = $_;
$i++;
}
foreach ( @save )
{
print $file_m "$_";
}
close( $file_m );
foreach ( @backup )
{
print $file_b "$_";
}
close( $file_b );
&stats( $i,$b,$g,$m_file,$b_file );
}
sub stats
{
my $comment = COMMENT ;
my( $all,$good,$bed,$file_m,$b_file ) = ( shift,shift,shift,shift,shift );
printf "\t In all the lines : %.3d \n".
"\t Lines Containing '%.4s' : %.3d \n".
"\t Lines not Containing '%.4s' : %.3d \n\n".
"\t Lines, Containing is stored in : %s \n".
"\t A back-up copy is stored in : %s \n",$all,$comment,$good,$comment,$bed,$file_m,$b_file ;
}
sub usage
{
print "\t Usage : $0 -f <file> \n".
"\t Example : $0 -f file.txt \n";
exit 1;
}
Мой файл : file.txt
Код:
*line1
line2
line3
*line4
запускаем : Статистика :
Код:
In all the lines : 005
Lines Containing '\x2A' : 002
Lines not Containing '\x2A' : 003
Lines, Containing is stored in : file.txt
A back-up copy is stored in : backup_file.txt
Смотрим файл:file.txt
бекап в файле backup_file.txt
Последний раз редактировалось demonoid; 20.09.2007 в 10:04..
|
|
|