Print a file in multiple columns based on delimiter

前端 未结 4 866
广开言路
广开言路 2021-01-23 08:37

This seems like a simple task, but using duckduckgo I wasn\'t able to find a way to properly do what I\'m trying to.

The main question is: How do I split the output of a

4条回答
  •  难免孤独
    2021-01-23 09:18

    The question is tagged as Perl so here is a possible Perl answer:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my $is_col1 = 1;
    my $in_block = 0;
    my @col1;
    
    while () {
        chomp;
        if (/^\s*-+\s*$/ ... /^\s*-+\s*$/) {
            $in_block = 1;
            if ($is_col1) {
                push @col1, $_;
            }
            else {
                printf "%-40s%-40s\n", shift @col1 // '', $_;
            }
    
        }
        else {
            if ($in_block) {
                $in_block = ! $in_block;
                $is_col1 = ! $is_col1;
                print "\n" if $is_col1; # line separating blocks
            }
        }
    }
    
    print join("\n", @col1), "\n\n" if @col1;
    
    __DATA__
    -----------------------------------
    Some data
    that varies in line length
    -----------------------------------
    
    -----------------------------------
    More data that is seperated
    by a new line and dashes
    with a longer column2
    -----------------------------------
    
    
    -----------------------------------
    The odd last column
    -----------------------------------
    

    Output:

    -----------------------------------     -----------------------------------
    Some data                               More data that is seperated
    that varies in line length              by a new line and dashes
    -----------------------------------     with a longer column2
                                            -----------------------------------
    
    -----------------------------------
    The odd last column
    -----------------------------------
    

提交回复
热议问题