in reply to Re: replace a column
in thread replace a column

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

Replies are listed 'Best First'.
Re^3: replace a column
by davidj (Priest) on Jul 06, 2004 at 15:03 UTC
    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
Re^3: replace a column
by Anonymous Monk on Jul 06, 2004 at 14:37 UTC
    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
      but will use the code u have provided, thanks!