Compare text file and a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare text file and a folder
# 1  
Old 06-11-2012
Compare text file and a folder

Hi,

I have a folder which contains some files
Code:
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

Code:
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

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

Please suggest.

Last edited by Scrutinizer; 06-11-2012 at 06:45 AM.. Reason: code tags
# 2  
Old 06-11-2012
Try:
Code:
grep -vxFf file2 file1

--
Please view this link to learn the use of code tags.
# 3  
Old 06-11-2012
grep -vxFf file1 file2 returned all the contents of the folder
.Didnot remove the matching files.
# 4  
Old 06-11-2012
OK, I misread, it is a folder, so then it should be:
Code:
ls | grep -vxFf file.txt

# 5  
Old 06-11-2012
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.
# 6  
Old 06-11-2012
Try this Perl code:
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

# 7  
Old 06-11-2012
Quote:
Originally Posted by arijitsaha
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.
That should not be the case, because of the -v option (which means invert match). What is your OS and version?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create new folder and text file the same time in one line

Is it possible to create new folder and write a new text file in the folder while the current position is outside the new folder? in one line mkdir folder | echo "hello test"> folder/test.txt not work. (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

Compare Values in a Delimited Text file

Hi, How do I compare two columns within a text file If 2nd column values are same then I want to know 3rd column number matches or not Example: Prod Stag1 1234.79 Prod Stag2 1234.79 20 Prod Stag3 1234.79 30 Prod Stag4 1234.79 UAT Stag1 1243.56 UAT Stag2 1243.56 20 UAT ... (3 Replies)
Discussion started by: krux_rap
3 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. Shell Programming and Scripting

Compare files in a folder based on another file

I have a file named file.txt that looks as follows //class1.txt 45 234 67 89 90 //class2.txt 456 34 78 89 120 class1 and class2.txt are the names of files in a folder named folder1. The content of class1.txt file in folder1 67 9 89 5 234 9The content of class2.txt file in... (1 Reply)
Discussion started by: jaff rufus
1 Replies

5. Shell Programming and Scripting

Compare filename to file folder

Hi. I have a problem, could you help me ? Situation: there are alot of files in directory_1, with the names like "notneeded1blabla.bla", "notveryneededblabla.gla". there is directory_2 that has directories with names like that: "bla_needed1", "blab_veryneeded". What i need so much is to place... (7 Replies)
Discussion started by: sw_and
7 Replies

6. UNIX for Dummies Questions & Answers

Compare and merging the differences in text file

Hi i have gone through some sdiff command it shows the differences side by side and its really awesome file 1: this tool is for checking the differ merging with flower pots documentation file 2: this t ool is for checking the differ mergin g with flower pots documentation ... (27 Replies)
Discussion started by: rakeshkumar
27 Replies

7. Shell Programming and Scripting

compare text in log file

Hi all, I am posting the thread similar to previous posts but here my scenario is i need to know what the files size from start date. Here end time file size will be 0. Also need to know how much time does it took to complete in seconds. log file: Name1 START 11:36:45 ... (5 Replies)
Discussion started by: Olivia
5 Replies

8. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

9. UNIX for Dummies Questions & Answers

How to compare the difference between a file and a folder??

Hi, I have a .txt file which has to be compared with a folder and print the difference to some other .txt file. I did try with the diff command..i mean diff /tmp/aaa/bbb.txt /space/aaa/bbb/ /***bbb.txt contains all the files names which may or may not exist in the folder bbb..so i need... (2 Replies)
Discussion started by: kumarsaravana_s
2 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question