Parse fixed-width files

后端 未结 3 1093
南方客
南方客 2021-01-12 00:09

I have a lot of text files with fixed-width fields:

            
Dave    Thomas    123 Main
Dan     Anderson  456 Center
Wilma   R         


        
3条回答
  •  灰色年华
    2021-01-12 00:33

    As user604939 mentions, unpack is the tool to use for fixed width fields. However, unpack needs to be passed a template to work with. Since you say your fields can change width, the solution is to build this template from the first line of your file:

    my @template = map {'A'.length}        # convert each to 'A##'
                    =~ /(\S+\s*)/g;  # split first line into segments
    $template[-1] = 'A*';                  # set the last segment to be slurpy
    
    my $template = "@template";
    print "template: $template\n";
    
    my @data;
    while () {
        push @data, [unpack $template, $_]
    }
    
    use Data::Dumper;
    
    print Dumper \@data;
    
    __DATA__
                
    Dave    Thomas    123 Main
    Dan     Anderson  456 Center
    Wilma   Rainbow   789 Street
    

    which prints:

    template: A8 A10 A*
    $VAR1 = [
              [
                'Dave',
                'Thomas',
                '123 Main'
              ],
              [
                'Dan',
                'Anderson',
                '456 Center'
              ],
              [
                'Wilma',
                'Rainbow',
                '789 Street'
              ]
            ];
    

提交回复
热议问题