Solved

Is it possible to write a Perl one-liner to get difference between two files?

Posted on 2009-07-13
3
244 Views
Last Modified: 2012-05-07
Have two files test1.txt and test2.txt
test1.txt
AA-A123
AABBCC
123_ABC
XYZ-A01
.....
test2.txt
123_ABC
AABBCC
AA-A123
....
Want to get test1.txt subtracts test2.txt , i.e.
XYZ-A01
You can assume that test2.txt is a part of test1.txt but the order is not necessarily same.
0
Comment
Question by:jl66
3 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 150 total points
ID: 24840280
Not a 1-liner, but this works....
##### Remove file1 from @ARGV (process file2 first)

my $f2=shift(@ARGV);
 

##### Read file2

my %file2;

while(<>) {

	chomp;

	$file2{$_}++;

}
 

push @ARGV, $f2;

while(<>) {

	chomp;

	next if $file2{$_};

	print "$_\n";

}

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 24841645
perl -ne "print unless $s{$_}++||@ARGV" test2.txt test1.txt
0
 

Author Comment

by:jl66
ID: 24856801
Thank both of you very much. I will verify the solutions later.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Uploading Binary Data using Perl 5 83
Merge files & delete row based on criteria using Perl. 1 100
How to strip .csv from file name 9 78
Awk Question 2 124
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now