Compare text file and a folder

Hi,

I have a folder which contains some files

a1.txt
a2.txt
a3.txt
a4.txt
a5.txt
a6.txt
a7.txt
a8.txt
a_index.txt

Also i have a text file which contains entries like this

a1.txt
a3.txt
a7.txt

I want to comapare this text file and folder and remove the similar values in both of them.

My output should be like this

a2.txt
a4.txt
a5.txt
a6.txt
a8.txt
a_index.txt

Please suggest.

Try:

grep -vxFf file2 file1

--
Please view this link to learn the use of code tags.

grep -vxFf file1 file2 returned all the contents of the folder
.Didnot remove the matching files.

OK, I misread, it is a folder, so then it should be:

ls | grep -vxFf file.txt

i want to remove all the matching entries. The output should contain only the non-matching files.

But in this it is returning all the matching rows.

Try this Perl code:

#!/usr/bin/perl

use strict;

#----------------
# Hash Definition
#----------------
my %HoA;
my @name;

#--------------------
# Subroutine
#--------------------
sub comp_file{
   my ($FILE1, $FILE2) = @_;
   open (R, $FILE1) or die ("Can't open file $FILE1");
     foreach my $FP1(<R>){
        chomp($FP1);     
        my ($k, $l) = split(/\s+/,$FP1);
        push @{$HoA{'$FP1'}{$k}},$l; 
     }
   close (R);
    
   open (P, $FILE2) or die ("Can't open file $FILE2");
     foreach my $FP2(<P>){
        chomp($FP2);     
        my ($k, $l) = split(/\s+/,$FP2);
        push @{$HoA{'$FP2'}{$k}},$l;
     }
   close (P);
    
   foreach my $key(keys %{$HoA{'$FP1'}}){
     if (!exists $HoA{'$FP2'}{$key}){
         foreach my $last(@{$HoA{'$FP1'}{$key}}){
           push (@name,"$key$last");       
         }    
     }    

   }
  
   print "$_\n" for (sort @name);
}

############MAIN MENU####################################
# Pre-check Condition
# if the input doesn't contain two(2) files, return help 
# USAGE: check.pl FILE1 FILE2
#########################################################


if ($#ARGV != 1){ 
    print "USAGE: $0 <FILE1> <FILE2> \n"; 
    exit;
} 
else {
    my ($FILE1, $FILE2)= @ARGV;
    &comp_file($FILE1, $FILE2);
}

##RESULT DATA#####
#a2.txt
#a4.txt
#a5.txt
#a6.txt
#a8.txt
#a_index.txt

That should not be the case, because of the -v option (which means invert match). What is your OS and version?

i am using Linux.

Linux 2.6.18-274.7.1.el5 #1 SMP x86_64 x86_64 x86_64 GNU/Linux

Hi,

ls | rm `grep -xFf file.txt` will works with deletions too.

Here x stand for exact match from file , f for input from file and F with Fixed string separated by new line.

-Pankaj

Askari can this be done using shell script instead of perl.

OK I get it, you want to physically remove the files, so then grep should list the files that are common to the folder and the file so -v should be left out.

ls | grep -xFf file.txt

would list the files that need to be removed..

Thank you all.
It worked.

# cat /FILEPATH/file
a1.txt
a3.txt
a7.txt
# awk 'NR==FNR{a[$1]++;system("ls -1|grep -v tmp.awk>tmp.awk")}NR!=FNR{if(!a[$1])print $1}' /FILEPATH/file tmp.awk
a2.txt
a4.txt
a5.txt
a6.txt
a8.txt
a_index.txt