将下一个读取前置与前一个读入行的信息一致

问题描述 投票:-1回答:1

开发平台:Ubuntu 17.10主要是命令行工作

工具:perl 5.26和postgresql 9.6

目标:转换文件,以便我可以\ COPY到postgresql

信息:行分隔符是#符号

数据库表列:id work composer artist conductor orchestra album_title

问题:将下一个信息行附加到当前id行

在下面我如何保留'mmfmm01#'所以在下一行迭代时我可以将它添加到下一行?由于这是我的第一篇文章,请告诉我代码示例是太多还是太少。

我要离开这个:

费加罗的婚姻,K。492 /费加罗的婚姻:费加罗的婚姻,K。492 /费加罗的婚姻:五......十......二十五......十......二十

最终到此:

mfmm01#Five five winds#Mozart #### Entertaining Made Simple Merlot,Filet Mignon,Mozart

运行脚本后,我有以下内容:

mfmm01#

我如何保留'mfmm01#'所以在下一行迭代时我可以将它添加到下一行?

    #!/usr/bin/perl 
    # use clauses for File, cwd, etc#
    # usage statment

    # Variables - append _orig to input_file
    my $id = $ARGV[1];
    my $input_file = $ARGV[0];
    my $album_title = $ARGV[2];
    my $output_file = output_file;
    my $input_file_orig = $input_file;
    $input_file_orig = $input_file_orig .= _orig;

    ##############################################
    # Ensure that the provided input file exists #
    ##############################################

    ##########################################################
    # Read all file lines into an array                      #
    ##########################################################

   ###########################################################
   # Modify each line to meet the following specs:           #
   # id#work#composer#artist#conductor#orchestra#album_title #
   ###########################################################
     for my $line (@lines) {
        $line =~ s/[\n\r\t]+//g;
        ######################################################
        # Ignore lines with num:num, lines that begin with $ #
        # and emptry string lines                            #
        # ####################################################
        if ( $line =~ /[0-9]:/m ) {
            next;
        } elsif ( $line =~ /^\$/m ) {
            next;
        } else {
        if ( $line =~ /^\s*$/m ) {
                next;
        }
        }
        ########################################################
        # If line is a number followed by a space, prepend id  #
        # and replace space with the # character               #
        ########################################################
        if ( $line =~ /^\d\d\s/m ) {
            $id_num = $line;
            $id_num =~ s/(\d\d)\s/$id$1#/g; 
        } else {
            if ( $line =~ /^\d\s/m ) {
            $id_num = $line;
            $id_num =~ s/(\d)\s/$id$1#/g;
   #        print ("\$line after removing space: \"$line\"\n");
        }
    }
    ####################################################
    # If line begins with an alphabetic character then #
    # prepend id_num and append album_title            #
    ####################################################
    if ( $line =~ /Sold/m ) {
        next;
    }
    if ( $line =~ /^[A-Z]/m ) {
        ################################################i##
    # At this point $line exists but $id_num is empty #
    # I thought $id_num would live through the next   #
    # line read                                       #
    ###################################################
           $prepend_line =~ s/($line)/$id_num$1/g; 
           print("$prepend_line");
           $append_line =~ s/($prepend_line)/$1#Mozart###$album_title/g;
           open my $ofh, '>>', $output_file or die $!;
           print $ofh "$append_line\n";
           close $ofh or die $!;
           print("\$append_line: $append_line\n");
        }
    }

    1;
postgresql perl
1个回答
0
投票

我将通过捕获文件的值来解决这个问题。在下一行读取时,我将提取值并将其添加到字符串之前。谢谢你看这个。我不知道为什么我之前没想过这个。

谢谢;

谢尔曼

© www.soinside.com 2019 - 2024. All rights reserved.