Save result to a text file

Currently I have a perl code to combine two different files.

#! /usr/bin/perl -w

use strict;

open FP1,"A.txt";
open FP2,"B.txt";
my ($l1,$l2);
while(1)
{
  $l1=<FP1>; chomp $l1;
  $l2=<FP2>; chomp $l2; 
  last unless(defined $l1 or defined $l2);
  print "$l1 $l2\n";
}

close FP2;
close FP1;

May I know how to save the result directly to A_B.txt?
So, I can only run

./paste

without need to

./paste > A_B.txt

Well that would be the general way to do it.

you can always add it into your perl script.

just add `./paste > A_B.txt`; to your code at the end.

1 Like
 
#! /usr/bin/perl -w
use strict;
open FP1,"A.txt";
open FP2,"B.txt";
open FP3, ">A_B.txt" or die $!;
my ($l1,$l2);
while(1)
{
  $l1=<FP1>; chomp $l1;
  $l2=<FP2>; chomp $l2; 
  last unless(defined $l1 or defined $l2);
  print FP3 "$l1 $l2\n";
}
close FP3;
close FP2;
close FP1;
2 Likes

Simple command in UNIX :- This command might be useful to you

 
paste file1 file2 > a_b.txt
 

Best Regards
Kalyan

thanks for the post itkamaraj