Compare string and eventually store

J

Jacks

Hi newsgroup I have two files, "file1" and "file2".
They contain string data like:

h:/test/test.test
i:/test2/test.test

Seperated by newlines.

I would like to read in one line from file1 and check whether this
line exists in somewhere in file2. If it doesnt I would like to write
the line in file1 that did not find at match in file2 in a file called
e.g. "diff.test"

I know that this is proparbly rather straight forward and a novice
question but could someone please help me?

Best Regards
J
 
X

xhoster

Jacks said:
Hi newsgroup I have two files, "file1" and "file2".
They contain string data like:

h:/test/test.test
i:/test2/test.test

Seperated by newlines.

I would like to read in one line from file1 and check whether this
line exists in somewhere in file2. If it doesnt I would like to write
the line in file1 that did not find at match in file2 in a file called
e.g. "diff.test"

How big is "file2"? If small (relative to your RAM), just create a hash
holding each line of file2. Then for each line of file1, probe the hash.

Xho
 
J

jl_post

Jacks said:
Hi newsgroup I have two files, "file1" and "file2".
They contain string data like:

h:/test/test.test
i:/test2/test.test

Seperated by newlines.

I would like to read in one line from file1 and check whether this
line exists in somewhere in file2. If it doesnt I would like to write
the line in file1 that did not find at match in file2 in a file called
e.g. "diff.test"

I know that this is proparbly rather straight forward and a novice
question but could someone please help me?


If the files are not tremendously huge, I would recommend reading
the lines of each file into separate arrays, and then printing the
difference of the arrays. If you can figure out how to read the lines
of each file into its own array, you can find the difference by reading
the information you get when you type:

perldoc -q "compute the difference"

at the command line.

Just for fun, I decided to write a simple one-liner that should do
what you want (it's one whole line):

perl -e "{local @ARGV=shift; @h{<>}=()} delete @h{<>}; print sort
keys %h" file1 file2 > diff.test

-- Jean-Luc
 
B

Brian McCauley

Jacks said:
I would like to read in one line from file1 and check whether this
line exists in somewhere in file2. If it doesnt I would like to write
the line in file1 that did not find at match in file2 in a file called
e.g. "diff.test"

Much as I like Perl and even though this is a Perl newshroup this is
really a job for grep not perl:

grep -Fxvf file2 file1 >diff.test

This is using GNU grep. Some lesser implementations may not support all
those switches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top