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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.