Using perl to parse a file and insert certain values ​​into a database

Disclaimer: I'm new to perl scripting, this is partly a training exercise (but still a project to work with). In addition, I have a much stronger understanding of shell scripts, so my examples will most likely be formatted in this way of thinking (but I would like to create them in perl). Sorry in advance for my verbosity, I want to make sure that I am at least very clear in order to understand my point of view.

I have a text file (reference guide) which is a Word document converted to text and then replaced from Windows to UNIX format in Notepad ++. The file is homogeneous, since each section of the file has the same fields / formatting / tables.

What I intended to do in the main way was to capture each section using unique batch job names and put all the values ​​in the database (or maybe just an excel file) so that all fields could be searched / edited for each job much easier than in a word file, and maybe later create a web interface.

So what I want to do is grab each section by doing something like:
sed -n '/job_name_1_regex/,/job_name_2_regex/' file.txt- how will it be formatted in a perl script?
(grab the plot as a whole, then tear it further)

To read the file in a script I have open FORMAT_FILE, 'test_format.txt';, and then use foreach $line (<FORMAT_FILE>)to parse the file line by line. - Is there a better way?

My next problem is that since I converted from the word doc to tables, it looks like this:

 Table Heading 1 Table Heading 2
Heading 1/Value 1    Heading 2/Value 1
Heading 1/Value 2    Heading 2/Value 2

:

Table Heading 1 
Table Heading 2
Heading 1/Value 1
Heading 1/Value 2
Heading 2/Value 1
Heading 2/Value 2

, " 1" " 2" . , . 1 1 2 ( 1, 2, 1). , awk/sed , , perl script.

--- EDIT ---
:
my @heading1 = ($value1, $value2, etc.)
my @heading2 = ($value1, $value2, etc.)

. , 1 = 2 ( ). ( ):
x=$(cat file.txt | grep -n "Heading 1" | cut -d":" -f1) - , " 1"
(( x = x+2 )) - 2 ( )
sed -n "$x,$last_line_of_values p" file.txt - file.txt , ( - )

- , , ... , ...
---/---

, .., , , perl .

, ... .

+3
2

http://perlmeme.org/tutorials/connect_to_db.html

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

my $driver = "mysql";   # Database driver type
my $database = "test";  # Database name
my $user = "";          # Database user name
my $password = "";      # Database user password

my $dbh = DBI->connect(
    "DBI:$driver:$database",
    $user, $password,
    {
        RaiseError => 1,
        PrintError => 1,
    }
) or die $DBI::errstr;

my $sth = $dbh->prepare("
        INSERT INTO test 
                    (col1, col2)
             VALUES (?, ?)
    ") or die $dbh->errstr;

my $intable = 0;
open my $file, "file.txt" or die "can't open file $!";
while (<$file>)  {
  if (/job_name_1_regex/../job_name_2_regex/) { # job 1 section
    $intable = 1 if /Table Heading 1/; # table start
    if ($intable) {
      my $next_line = <$file>; # heading 2 line
      chomp; chomp $next_line;
      $sth->execute($_, $next_line) or die $dbh->errstr;
    }
  }
}
close $file or die "can't close file $!";
$dbh->disconnect;
+3

... -, " ":

  • perl.

    use strict; use warnings;

  • , ( ).

  • "" .

    open my $file, "/some/file" or die "can't open file : $!"

, : , , - :

foreach my $line ( <$file> ) {
    if ( $line =~ /regexp1/) { 
    # do something...
    }

}

: , , , . , @col1, - @col2. :

my ( $val1, $val2 ) = split /\s+/, $line;
push @col1, $val1;
push @col2, $val2;
+2

Source: https://habr.com/ru/post/1742309/


All Articles