Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I need to replace an entire column with a column from another file; replace
x y z2 y
with column from another file:
x y x2 y2
but in the same format as the orginal, any suggestions would be great. I thought of removing the entire column but this would remove the format needed? Thanks,

Replies are listed 'Best First'.
Re: replace a column
by pbeckingham (Parson) on Jul 06, 2004 at 13:45 UTC

    Would you elaborate on your question please? Specifically, if you could provide a couple of rows of data, and indicate what form it currently has. Perhaps it is on disk, perhaps in an AoA, or AoH...

    I don't think we can help without more information.

      yes, think your right i have replaced the column with white space that needs replacing: input file:
      0.00 X 1.000 0.530 X 0.753 # output values for residue 1 0 # input 0.00 X 1.00 0.555 X 0.683 # output values for residue 2 0
      so replace the X's with data from oter file:
      0.99 0.99 0.99 0.99
      i thought something along the lines
      open (FILE, "<filetoedit"); open (NEED, "<insert"); open (OUT, ">dine"); @array = <NEED>; $end = scalar @array; $count = 0; while (<FILE>) { for ($count = 0; $count < $end; $count++) { ###then some how replace x's with the new values
Re: replace a column
by davidj (Priest) on Jul 06, 2004 at 14:14 UTC
    First, this solution is a simple hack and most likely not the most efficient.

    Second, it makes some assumptions about what you mean by "in the same format as the original", namely that:
    1) you want to retain the blank values in the original file even if the corresponding column in the replacement file is not blank.
    2) you do not skip the value in the replacement column if the corresponding value in the original file is blank.
    ( are we confused yet :) )

    given the following original file:
    1:2:3:4 5:6:7:8 :9:10:11 12:13:14:15 :17:18:19
    and the following replacement file:
    a:b:c:d:e f:g:h:i:j k:l:m:n:o p:q:r:s:t u:v:w:x:y
    The following code replace the first column (for simplicity sake) in the original file with the first column in the replacement file (according to the assumptions about your requirements I made above).
    use File::Copy; use strict; my (@col, $val, $i); open(COL, "<replace.txt"); while(<COL>) { push @col, $_ =~ m/(.*?):/; } close(COL); open(OUT, ">out.txt"); open(COL, "<original.txt"); while(<COL>) { ($val) = $_ =~ m/(.*?):/; $_ =~ s/(.*?)(?=:)/$col[$i++]/ unless $val =~ m/^\s*$/; print OUT $_; } close(COL); close(OUT); copy("out.txt", "original.txt");
    contents of orginal file after program is run:
    a:2:3:4 f:6:7:8 :9:10:11 k:13:14:15 :17:18:19
    Again, this may or may not be what you want since you were not specific in the meaning of "in the same format as the original".

    davidj
      essentially:
      0.00 X 1.000 0.530 X 0.753 # output values for residue 1 0 # input 0.00 X 1.00 0.555 X 0.683 # output values for residue 2 0 would become 0.00 0.99 1.000 0.530 0.99 0.753 # output values for residue 1 0 # input 0.00 0.99 1.00 0.555 0.99 0.683 # output values for residue 2 0
        Ok, if you simply want to replace the values of 1 arbitrary column in 1 file with the values of 1 arbitrary column in another file, the modified version of what I previously posted will do it.

        given the following original file:
        1:2:3:4 5:6:7:8 9:10:11:12 13:14:15:16 17:18:19:20
        and the following replacement file:
        a:b:c:d:e f:g:h:i:j k:l:m:n:o p:q:r:s:t u:v:w:x:y
        The following example will replace column 2 of the orginal file with column 3 of the replacement file
        #!/usr/bin/perl use File::Copy; use strict; my (@col, @vals, $val, $i); open(COL, "<replace.txt"); while(<COL>) { chomp($_); (@vals) = split(":", $_); push @col, $vals[1]; #store values for column 2 } close(COL); open(OUT, ">out.txt"); open(COL, "<original.txt"); while(<COL>) { chomp($_); (@vals) = split(":", $_); $vals[2] = $col[$i++]; #replace values in column 3 print OUT join(":", @vals) . "\n"; } close(COL); close(OUT); copy("out.txt", "original.txt");
        original file after running the program:
        1:2:b:4 5:6:g:8 9:10:l:12 13:14:q:16 17:18:v:20

        davidj
        i ahve tried
        while (<FILE>) { #foreach $line = <FILE> { for ($count = 0; $count < $end; $count++) { s/^(\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+)\s+(.*)/$1 $array[$coun +t] $2/; } print OUT $_; } but the entire array is printed at the substitution point? # input 0.00 1.000 0.00 0.00 1.00 0.00 1.00 0.99 0.99 0.710 0.000 0.22 0.780 0.61 0.39 0.17 0.00 0.50 0.99 0.99 0.530 0.753 # output values for residue 1 0 # input 0.00 1.000 1.00 1.00 0.00 0.00 0.00 0.99 0.99 0.264 0.000 0.22 0.780 0.56 0.33 0.22 0.00 0.56 0.99 0.99 0.555 0.683 # output values for residue 2 0
Re: replace a column
by wfsp (Abbot) on Jul 06, 2004 at 15:43 UTC
    Hi,
    If I understand you correctly, this maybe worth a look.
    This uses a regex to only change records that contain the column you want to change. Other records are passed through unchanged.
    If the 'x' doesn't actually appear in the data just remove it from the regex. In that case there would need to be at least 2 spaces or tabs in the middle for it to work.
    Give us more examples of live data if I've misunderstood you. update: fixed typo