CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
save text of one file using text of another file

 



cmccabe1
Novice

Mar 24, 2015, 12:42 PM

Post #1 of 4 (3697 views)
save text of one file using text of another file Can't Post

I am trying to use the text of one file as a text file name with and the text of another as the contents of that text file. Is this possible in perl? Thank you :).


Code
  
For example, in the two files attached, target.txt has:
1.txt
2.txt
and out_parse.txt has:
13 20763642 20763642 C G
13 20763438 20763438 C G

What I can not seem to do is use the first line of target.txt as the text file name and combine it with the first line of out_parse.txt and so on for the second.

Desired output:
1.txt
13 20763642 20763642 C G

2.txt
13 20763438 20763438 C G



Thank you :).
Attachments: out_parse.txt (50 B)
  target.txt (12 B)


FishMonger
Veteran / Moderator

Mar 24, 2015, 1:36 PM

Post #2 of 4 (3695 views)
Re: [cmccabe1] save text of one file using text of another file [In reply to] Can't Post

What have you tried?

What errors/warnings are you receiving?


cmccabe1
Novice

Mar 24, 2015, 1:40 PM

Post #3 of 4 (3691 views)
Re: [FishMonger] save text of one file using text of another file [In reply to] Can't Post

I figured it out in awk:


Code
 awk '{close(fname); getline fname<f; print>fname}' f=target.txt out_parse.txt


and was trying to do the same using perl. Thanks :).


Laurent_R
Veteran / Moderator

Mar 25, 2015, 12:57 AM

Post #4 of 4 (3647 views)
Re: [cmccabe1] save text of one file using text of another file [In reply to] Can't Post

Assuming you have opened the two files as $IN1 and $IN2 you could do this:

Code
while (my $line1 = <$IN1>) { 
print $line1;
my $line2 = <$IN2>;
print $line2;
}

There is more to it: for example, you might need to check whether $line2 is defined and decide what to do if it is not defined, but you did not give enough details.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives