Re: OT: a problem about text manipulation

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

 



On Tue, 31 Oct 2006, Paul Smith wrote:

> Dear All
> 
> I have a text file with more than 300000 of lines like the following:
> 
> 1	E	E	E
> 2	C	D	3	B	E	D
> 4	C	E	D
> 5		D	E
> 
> I am looking for an automatic way of transforming my text file into
> another one with the format
> 
> 1	E
> 1	E
> 1	E
> 2	C
> 2	C
> 3	B
> 3	B
> 3	D
> 4	C
> 4	C
> 4	D
> 5	E
> 
> Any ideas?

Your input data seems to not actually be equivalent to your output example 
(at least not in any obvious way).
 
Assuming that what you *MEANT* for your output example was

1 E
1 E
1 E
2 C
2 D
3 B
3 E
3 D
4 C
4 E
4 D
5 D
5 E

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";
        }
    }
}


[me@mysystem] ./scriptname.pl data_file.txt

-- 
Benjamin Franz

"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

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux