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 8042329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:32:31+00:00 2026-06-05T04:32:31+00:00

I have 2 files, a small one and a big one. The small file

  • 0

I have 2 files, a small one and a big one. The small file is a subset of the big one.

For instance:

Small file:

solar:1000
alexey:2000

Big File:

andrey:1001
solar:1000
alexander:1003
alexey:2000

I want to delete all the lines from Big.txt which are also present in Small.txt. In other words, I want to delete the lines in Big file which are common to the small File.

So, I wrote a Perl Script as shown below:

#! /usr/bin/perl

use strict;
use warnings;

my ($small, $big, $output) = @ARGV;

open(BIG, "<$big") || die("Couldn't read from the file: $big\n");
my @contents = <BIG>;
close (BIG);

open(SMALL, "<$small") || die ("Couldn't read from the file: $small\n");

while(<SMALL>)
{
    chomp $_;
    @contents = grep !/^\Q$_/, @contents;
}

close(SMALL);

open(OUTPUT, ">>$output") || die ("Couldn't open the file: $output\n");

print OUTPUT @contents;
close(OUTPUT);

However, this Perl Script does not delete the lines in Big.txt which are common to Small.txt

In this script, I first open the big file stream and copy the entire contents into the array, @contents. Then, I iterate over each entry in the small file and check for its presence in the bigger file. I filter the line from Big File and save it back into the array.

I am not sure why this script does not work? Thanks

  • 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-06-05T04:32:34+00:00Added an answer on June 5, 2026 at 4:32 am

    Your script does NOT work because grep uses $_ and takes over (for the duration of grep) the old value of your $_ from the loop (e.g. the variable $_ you use in the regex is NOT the variable used for storing the loop value in the while block – they are named the same, but have different scopes).

    Use a named variable instead (as a rule, NEVER use $_ for any code longer than 1 line, precisely to avoid this type of bug):

    while (my $line=<SMALL>) {
        chomp $line;
        @contents = grep !/^\Q$line/, @contents;
    }
    

    However, as Oleg pointed out, a more efficient solution is to read small file’s lines into a hash and then process the big file ONCE, checking hash contents (I also improved the style a bit – feel free to study and use in the future, using lexical filehandle variables, 3-arg form of open and IO error printing via $!):

    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my ($small, $big, $output) = @ARGV;
    
    use File::Slurp;
    my @small = read_file($small);
    my %small = map { ($_ => 1) } @small;
    
    open(my $big, "<", $big) or die "Can not read $big: Error: $!\n";
    open(my $output, ">", $output) or die "Can not write to $output: Error: $!\n";
    
    while(my $line=<$big>) {
        chomp $line;
        next if $small{$line}; # Skip common
        print $output "$line\n";
    }
    
    close($big);
    close($output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big xml database (30 000 files, 1.3 Go). One file in
I have file about 4MB (which i called as big one)...this file has about
Say I have a big file in one directory and an older version of
Hey! Let's say I have a big application in one .html file. Another file,
I have one big div with id=elements and I load from JSON file new
In my application I have small tables(files) with unique keys (most of those have
I need to edit XML files on a small Linux box that we have
I have small web app that generate PDF files as a report. I'm trying
I have a small app that downloads some files from a remote (HTTP) server
we have a small flash component on our website/application to upload multiple files. This

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.