Solved

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

Posted on 2009-07-13
3
229 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
Comment Utility
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
Comment Utility
perl -ne "print unless $s{$_}++||@ARGV" test2.txt test1.txt
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

10 Experts available now in Live!

Get 1:1 Help Now