Re: OT: a problem about text manipulation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 31 Oct 2006, Benjamin Franz wrote:

> the following Perl script should do it.
> 
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> while (<ARGV>) {
>     chomp;
>     my @elements = split(/\s+/);
>     my $running_col1;
>     foreach my $item (@elements) {
>         if ($item =~ m/^[0-9]+$/) {
>             $running_col1 = $item;
>         } else {
>             print "$running_col1 $item\n";
>         }
>     }
> }
> 

Here is a slightly revised version that would handle the case if a line 
break occured in the middle of a 'set'. IOW it would handle the case of

1   a   b   c
d   e   2   a   a
3   e   w   a

#!/usr/bin/perl

use strict;
use warnings;

my $running_col1;
while (<ARGV>) {
    chomp;
    my @elements = split(/\s+/);
    foreach my $item (@elements) {
        if ($item =~ m/^[0-9]+$/) {
            $running_col1 = $item;
        } else {
            print "$running_col1 $item\n";
        }
    }
}
-- 
Jerry

"It is moronic to predict without first establishing an error rate
 for a prediction and keeping track of one’s past record of accuracy."
                    -- Nassim Nicholas Taleb, Fooled By Randomness
-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux