biscardi has asked for the wisdom of the Perl Monks concerning the following question:
starting form the top of the file I need 1. to get the the value in the 3rd column (c1)a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4
2. search in a second file (file_B.txt, not tab delimited and quite messy) all the matches for it.
3. when a match is found, I would like to append to the current value (c1), the value of 4th column (d1) in file_A.txt, separated by a space.
4. go back to the first file (file_A.txt), get the the value in the 3rd column in the second row (c2) and do another round of search and insert the value of d2 in the second file (file_B.txt).
5. go ahead with the search and replace until the end of file_A.txt is reached. I am an absolute newbee and I put together this code that does not work very well.
1. I don`t know how to tell perl to loop until the end of file_A.txt, so I have just used a for statement#!/usr/bin/perl open (datafile, "file_A.txt"); @fileinput = split("\t", <datafile>); for ($i = 2; $i <=200;){ open(OF, "file_B.txt"); #file_B.txt contains the original file open(NF, ">file_B_out.txt"); #file_B_out.txt contains the processe +d output while ($line = <OF>) { print "$fileinput[$i]\n"; print "$i\n"; $line =~ s/$fileinput[$i]/$fileinput[$i+1]/g; #print $line; print NF $line; } $i=$i+4; } close(NF); close(OF);
2. the search and replace routine does not work and I don`t understand why
Any suggestions and possibly example would be really appreciated
thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get data from a file to search and replace within a second file
by almut (Canon) on Mar 23, 2010 at 00:19 UTC | |
|
Re: Get data from a file to search and replace within a second file
by GrandFather (Saint) on Mar 22, 2010 at 23:59 UTC | |
|
Re: Get data from a file to search and replace within a second file
by toolic (Bishop) on Mar 23, 2010 at 00:23 UTC | |
by biscardi (Initiate) on Mar 23, 2010 at 15:48 UTC | |
by toolic (Bishop) on Mar 23, 2010 at 16:41 UTC | |
by educated_foo (Vicar) on Mar 24, 2010 at 14:21 UTC |