in reply to Re^2: replace a column
in thread replace a column
and the following replacement file:1:2:3:4 5:6:7:8 9:10:11:12 13:14:15:16 17:18:19:20
The following example will replace column 2 of the orginal file with column 3 of the replacement filea:b:c:d:e f:g:h:i:j k:l:m:n:o p:q:r:s:t u:v:w:x:y
original file after running the program:#!/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");
1:2:b:4 5:6:g:8 9:10:l:12 13:14:q:16 17:18:v:20
|
|---|