in reply to Re: Cutting text from the end of the file and adding it to the top
in thread Cutting text from the end of the file and adding it to the top

Hi, Thank you for your quick response. By all rows need to be filled, I meant that the same logic needs to be used to fill up all the rows. The top level algo will be: 1. Calculate number of columns from the header. 2. Start splitting the last row in the file by the number of columns. 3. Paste the splits starting from the second row. I tried the script and it is not giving the desired output. Input:
a b c d e f g h aa bb cc dd aa bb cc dd aa bb cc dd 1 2 3 4 5 6 7 8 9 10 11 12 13 1 4 15 16
Output with your script snippet:
a b c d e f g h aa 4 15 16 bb cc dd aa bb cc dd aa bb cc dd 1 2 3 4 5 6 7 8 9 10 11 12 13 1
Desired:
a b c d e f g h aa 1 2 3 4 5 6 7 8 bb 9 10 11 12 13 14 15 16 cc 17 18 19 20 21 22 23 24 dd 25 26 27 28 29 30 31 32 aa 33 34 35 36 37 38 39 40 bb 41 42 43 44 45 46 47 48 cc 49 50 51 52 53 54 55 56 dd 57 58 59 60 61 62 63 64 aa 65 66 67 68 69 70 71 72 bb 73 74 75 76 77 78 79 80 cc 81 82 83 84 85 86 87 88 dd 89 90 91 92 93 94 95 96 1 2 3 4 5 6 7 8 9 10 11 12 13 1 +4 15 16 17 18 19 20 21 22 23 24 25 + 26 27 28 29 30 31 32 33 34 35 36 3 +7 38 39 40 41 42 43 44 45 46 47 48 + 49 50 51 52 53 54 55 56 57 58 59 6 +0 61 62 63 64 65 66 67 68 69 70 71 + 72 73 74 75 76 77 78 79 80 81 82 8 +3 84 85 86 87 88 89 90 91 92 93 94 + 95 96

Replies are listed 'Best First'.
Re^3: Cutting text from the end of the file and adding it to the top
by hippo (Archbishop) on Sep 30, 2019 at 10:15 UTC

    So, you are saying that the numbers in the input are essentially irrelevant. SSCCE:

    #!/usr/bin/env perl use strict; use warnings; my @lines = <DATA>; print shift @lines; chomp @lines; my $perline = 8; my @num; my @numline = (1 .. $perline); my $fmt = '%s ' . '%5i' x $perline . "\n"; for my $line (@lines) { if ($line =~ /^1/) { print join (' ', @num) . "\n"; last; } printf $fmt, $line, @numline; push @num, @numline; $_ += $perline for @numline; } __DATA__ a b c d e f g h aa bb cc dd aa bb cc dd aa bb cc dd 1 2 3 4 5 6 7 8 9 10 11 12 13 1 +4 15 16
      Thanks, no I just gave an example of serial numbers, but the numbers can be any random numbers and the numbers are relevant. example: Input:
      a, b, c, d, aa, bb, aa, bb, 12,33,102,111,223,24,88,56,99,130,21,45,88,90,212,500
      Desired output:
      a, b, c, d, aa, 12, 33, 102, 111, bb, 223, 24, 88, 56, aa, 99, 130, 21, 45, bb, 88, 90, 212, 500,

        What is the format of your text file ?

        In your original post it was whitespace delimiter with all data on the last line. On this post your data is over the last 2 lines (which is why Athanasius's script does not work for you) and on your latest post the data is comma delimited.

        poj
        $ cat tmp/tmp.pl && perl tmp/tmp.pl #!/usr/bin/perl # use strict; use warnings; my $heading=readline(DATA); my @headings=split/,/,$heading; print $heading; my (@index,@values); while(<DATA>){ chomp; if (/^(\w+),\s*$/){ push @index,$1; } else{ @values=split/,/,$_; } } my $row = 0; for my $record (@index){ print join(",\t", $record, @values[(scalar(@headings) * $row) .. ( +(scalar(@headings) * $row) + $#headings -1)]),"\n"; $row++; } __DATA__ a, b, c, d aa, bb, aa, bb, 12,33,102,111,223,24,88,56,99,130,21,45,88,90,212,500 a, b, c, d aa, 12, 33, 102 bb, 223, 24, 88 aa, 99, 130, 21 bb, 88, 90, 212
        I trimmed the trailing "," in your headings line as that breaks the CSV standard, but your format seems variable so...
        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."