Guest User

Untitled

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