Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6185299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:41:13+00:00 2026-05-24T01:41:13+00:00

I have a script to compare 2 files and print out the matching lines

  • 0

I have a script to compare 2 files and print out the matching lines on the file. what I want to add a logic to help me to identify for how long these devices are matched. currently I have add the starting point 1 so I want to increase that number every time the script run and matched.

Example.

inputfile:-########################

retiredDevice.txt

Alpha

Beta

Gamma

Delta

prodDevice.txt

first

second

third

forth

Gamma

Delta

output file :-#######################

final_result.txt

1 Delta

1 Gamma

my objective is to add a counter stamp on each matching line to identify for how long “Delta” and “Gamma” matched. the script running every week. so every time the script running adding 1 so when I audit the ‘finalResult.txt. the result should looks like

Delta 4

Gamma 3

the result indicate me Delta matched for last 4 weeks and Gamma for last 3 weeks.

#! /usr/local/bin/perl 
my $ndays = 1;
my $f1 = "/opt/retiredDevice.txt ";
my $f2 = "prodDevice.txt";
my $outfile = "/opt/final_result.txt";
my %results = ();

open FILE1, "$f1" or die "Could not open file: $! \n";
while(my $line = <FILE1>){   $results{$line}=1;
}
close(FILE1); 
open FILE2, "$f2" or die "Could not open file: $! \n";
while(my $line =<FILE2>) {  
$results{$line}++;
}
close(FILE2); 

open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) { 
my $x = $ndays;
$x++;
print OUTFILE "$x : ", $line if $results{$line} != 1;
}
close OUTFILE;

Thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:41:14+00:00Added an answer on May 24, 2026 at 1:41 am

    Based on your earlier question and comments, perhaps this might work.

    use strict;
    use warnings;
    use autodie;
    
    my $logfile = 'int.txt';
    my $f1 = shift || "/opt/test.txt";
    my $f2 = shift || "/opt/test1.txt";
    my %results;
    open my $file1, '<', $f1;
    while (my $line = <$file1>) {
        chomp $line;
        $results{$line} = 1;
    }
    open my $file2, '<', $f2;
    while (my $line = <$file2>) {
        chomp $line;
        $results{$line}++;
    }
    
    { ############ added part
        my %c;
        for (keys %results) {
            $c{$_} = $results{$_} if $results{$_} > 1;
        }
        %results = %c;
    } ############ end added part
    
    my (%log, $log);
    if ( -e $logfile ) {
        open $log, '<', $logfile;
        while (<$log>) {
            my ($num, $key) = split;
            $log{$key} = $num;
        }
    }
    
    open $log, '>', $logfile or die $!;
    for my $key (keys %results) {
        my $old = ( $log{$key} || 0 ); # keep old count, or 0 otherwise
        my $new = ( $results{$key} ? 1 : 0 ); # 1 if it exists, 0 otherwise
        print $log $old + $new, " $key\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script which is compare 2 files and print out the diff
I have a script that loops through many url based XML files to compare
I am trying to compare two decimal values in Java script. I have two
I have script file where a command is stored in a variable First i
I have a script that takes a table name and generates a control file
I have Beyond Compare 3 installed at; C:\Program Files\Beyond Compare 3\BCompare.exe and Cygwin; C:\Cygwin\bin\bash.exe
I have a script that reads two csv files and compares them to find
This is my first script, and I am trying to compare two genome files,
I want to count the number of common lines that exist between 2 files
I currently have the following lines of code in a script: set -A ARRAY

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.