Extracting data from a file with multi-line records,saving each range to a separate file

前端 未结 1 1843
小蘑菇
小蘑菇 2021-01-28 01:17

I have a file that looks like this

     *TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************5845
DATE 01-02-16    TIME 05:45:52
 05:46:26 GENAC 1 :         


        
相关标签:
1条回答
  • 2021-01-28 02:11

    @Iord-Ivan Try to study

    what is the datatype.?

    File handling

    What is handler.

    Your $somefile store only the file name. Then you iterate the while loop using $somefile. But your $somefile contain only the file name not contain the file content.

    You file content is stored into the $fh handler so iterate the while loop for $fh handler. Then check it line by line.

    use warnings;
    use strict;
    my $somefile = "input.txt";
    open (my $fh, '<:encoding(UTF-8)', $somefile) or die "Could not open file '$somefile' $!"; 
    print "$somefile open";
    while (<$fh>) {
    
        if (/TRANSACTION STARTED/ .. /TRANSACTION END/) 
        {
             next if /TRANSACTION\s*(STARTED|END)/;
            print $_;   
        }
    }
    

    After studying perl, finish the balance program(FILE WRITE). :)

    0 讨论(0)
提交回复
热议问题