Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.77 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use utf8;
  4. use DBI;
  5. use Encode;
  6.  
  7. my @files = ('/etc/asterisk/ABC-3x.html','/etc/asterisk/ABC-4x.html','/etc/asterisk/ABC-8x.html','/etc/asterisk/DEF-9x.html');
  8.  
  9. my $db_user = 'log';
  10. my $db_password = 'pass';
  11. my $db_database = 'asterisk';
  12. my $db_host = 'localhost';  
  13.  
  14. my ($base);
  15.  
  16. ;и перед началом импорта, надо базу очистить
  17. $base = DBI->connect( "DBI:mysql:database=$db_database;host=$db_host", $db_user, $db_password ) or die "Не могу подключиться к базе: ".$base->errstr."\n";
  18.  
  19. $base->do('TRUNCATE `def_codes`');
  20. foreach my $parse_file (@files) {
  21.     open (HTML, "cat  $parse_file | iconv -f cp1251 -t utf8 |");
  22.     while (<HTML>) {
  23.         chomp;
  24.         s/\t//g;
  25.         if ( $_ =~ /^<tr><td>/){
  26.             s/(<td>)|(<\/td>)|(<tr>)|(<\/tr>)/\t/g;
  27.             s/\t{2,}/\t/g;
  28.             my (undef, $t_def_code, $t_def_start, $t_def_stop, $t_def_num, $t_def_operator, $t_def_region) = split /\t/, $_;
  29.             my $stament = $base->prepare('INSERT INTO `def_codes`(`def_code`, `def_start`, `def_stop`, `def_count`, `def_operator`, `def_region`) VALUES ( '.$t_def_code.', '.$t_def_start.', '.$t_def_stop.', '.$t_def_num.', \''.$t_def_operator.'\', \''.$t_def_region.'\')');
  30.             if (!$stament) {
  31.                 print "Не могу подготовить запрос к выполнению:". $base->errstr."\n";
  32.                 next;
  33.             }
  34.             if ( !$stament->execute() ){
  35.                 print "Не могу выполнить запрос к базе: ".$stament->errstr."\n";
  36.                 next;
  37.             }
  38.             print $t_def_code."\t".$t_def_start."\t".$t_def_stop."\t".$t_def_operator."\t".$t_def_region."\n";
  39.         }
  40.     }
  41.     close(HTML);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement