This is my problem: I have a tab delimited file (file_A.txt) like this
a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4
starting form the top of the file I need 1. to get the the value in the 3rd column (c1)

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.

#!/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);
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

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.


In reply to Get data from a file to search and replace within a second file by biscardi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.