Sponsored Content
Full Discussion: How to fix my code in Perl?
Top Forums Shell Programming and Scripting How to fix my code in Perl? Post 302839695 by askari on Friday 2nd of August 2013 05:34:15 PM
Old 08-02-2013
How to fix my code in Perl?

Hi Perl users,

Could you help me how to fix my code so I can get the expected output as seen below?
is there missing in my program?

Thank You

Code:
#!/usr/bin/perl -w

use strict;

open (FH, "< file1.log") or die $!;
open (FL, "< file2.log") or die $!;

my $file = "result.log";

my ($afile, $bfile);
foreach my $lines (<FH>){
  chomp $lines;
  ($afile, $bfile) = split (/\s+/,$lines);
   if (open(FC, "> $file")){
     while (<FL>){
        if ($_ =~ /$afile/){
          print FC $_;
        }
     }
   }
   close (FC);               
}

close (FH);
close (FL);

__DATA__
file1.log:
unix1 test
unix2 test

file2.log:
unix1 halo halo
unix2 halo halo
unix3 halo halo

------

Code:
Unexpected result:
unix1 halo halo

Code:
The expected result:
unix1 halo halo
unix2 halo halo

---------- Post updated 08-03-13 at 05:34 AM ---------- Previous update was 08-02-13 at 10:47 PM ----------

Done by my self. Here is the code:
Code:
#!/usr/bin/perl -w  use strict;  open (FH, "< file1.log") or die $!; open (FL, "< file2.log") or die $!; my @temp = <FL>; my $file = "result.log";  my ($afile, $bfile); foreach my $lines (<FH>){   chomp $lines;   ($afile, $bfile) = split (/\s+/,$lines);    if (open(FC, "> $file")){      foreach (@temp){         if ($_ =~ /$afile/){           print FC $_;         }      }    }    close (FC);                }  close (FH); close (FL);

 

2 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to fix :[too many arguments error in code

I am getting a :; then echo "Enter zero or one file" echo "You must use a valid directory" echo "Current directory is:" pwd exit 0 fi #Flag Variable flag=1 #Code for no arguments if ; then for filename in * do if ; then ... (2 Replies)
Discussion started by: Brewer27
2 Replies

2. Shell Programming and Scripting

Help fix my garbage - File Split Program in Perl

Hi, I have the following, it doesn't work and I know it's crap code. The objective is to split a file with a givin number of codes such as: 01,02,03,...,99 Then return all records with each seperate identifier in a new file. The files being split have lrecl=500, recfm=F, and I... (4 Replies)
Discussion started by: mkastin
4 Replies
merge(1)																  merge(1)

NAME
merge - three-way file merge SYNOPSIS
merge [-Llabel1 [-Llabel3]] [-p] [-q] file1 file2 file3 DESCRIPTION
merge incorporates all changes that lead from file2 to file3 into file1. The result goes to standard output if -p is present, into file1 otherwise. merge is useful for combining separate changes to an original. Suppose file2 is the original, and both file1 and file3 are modifications of file2. Then merge combines both changes. An overlap occurs if both file1 and file3 have changes in a common segment of lines. On a few older hosts where diff3 does not support the -E option, merge does not detect overlaps, and merely supplies the changed lines from file3. On most hosts, if overlaps occur, merge out- puts a message (unless the -q option is given), and includes both alternatives in the result. The alternatives are delimited as follows: <<<<<<< file1 lines in file1 ======= lines in file3 >>>>>>> file3 If there are overlaps, the user should edit the result and delete one of the alternatives. If the -L label1 and -L label3 options are given, the labels are output in place of the names file1 and file3 in overlap reports. DIAGNOSTICS
Exit status is 0 for no overlaps, 1 for some overlaps, 2 for trouble. IDENTIFICATION
Author: Walter F. Tichy. Revision Number: 1.1.6.2; Release Date: 1993/10/07. Copyright (C) 1982, 1988, 1989 by Walter F. Tichy. Copyright (C) 1990, 1991 by Paul Eggert. SEE ALSO
diff3(1), diff(1), rcsmerge(1), co(1) merge(1)
All times are GMT -4. The time now is 04:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy