Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Athanasius (Archbishop)
on Sep 30, 2019 at 07:40 UTC ( [id://11106862]=note: print w/replies, xml ) Need Help??


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

Hello oysterperl, and welcome to the Monastery!

I assume the repetition of “8” in the desired output is a mistake? If so, the following script should give you an idea of how to proceed:

use strict; use warnings; my $header = <DATA>; chomp $header; $header =~ s/ ^ \s+ //x; my @columns = split /\s+/, $header; my $columns = scalar @columns; my @lines = <DATA>; my $data = pop @lines; chomp $data; my @data = split /\s+/, $data; print "\t", join("\t", @columns), "\n"; for my $line (@lines) { chomp $line; print $line; for my $i (1 .. $columns) { print("\t", shift @data) if @data; } print "\n"; } __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

Output:

17:37 >perl 2021_SoPW.pl 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 dd aa bb cc dd aa bb cc dd 17:37 >

I don’t know what you mean by “All the rows need to be filled” though.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Cutting text from the end of the file and adding it to the top
by oysterperl (Novice) on Sep 30, 2019 at 09:40 UTC
    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

      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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found