in reply to replace a column

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

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