Search/Replace in CSV files

D

Doug Wells

Hi-
I seem to be drawing a complete blank, can anyone help? I have two CSV
files. I want to look at file one and for each entry, if that entry
appears in the second file remove it from the second file. So, in the
end, i will have a new second csv file that has the first file items
removed.

Thanks
Doug
 
G

Gunnar Hjalmarsson

Doug said:
I seem to be drawing a complete blank, can anyone help? I have two
CSV files. I want to look at file one and for each entry, if that
entry appears in the second file remove it from the second file.

You may want to consider Tie::File for handling the second file.
 
C

ctcgag

Doug Wells said:
Hi-
I seem to be drawing a complete blank, can anyone help? I have two CSV
files. I want to look at file one and for each entry, if that entry
appears in the second file remove it from the second file. So, in the
end, i will have a new second csv file that has the first file items
removed.

Which part of this do you not know how to do? Do you not know how to
work with "CSV"[1] files? Do you not know how to work with hashes? Do you
not know how to use logic?


Xho

[1] Whatever that happens to mean in your context. Everyone's CSV seems to
be different.
 
D

Doug Wells

Doug Wells said:
Hi-
I seem to be drawing a complete blank, can anyone help? I have two
CSV files. I want to look at file one and for each entry, if that
entry appears in the second file remove it from the second file.
So, in the end, i will have a new second csv file that has the
first file items removed.

Which part of this do you not know how to do? Do you not know how to
work with "CSV"[1] files? Do you not know how to work with hashes?
Do you not know how to use logic?


Xho

[1] Whatever that happens to mean in your context. Everyone's CSV
seems to be different.

I would guess that I'm struggling with the logic - I understand hashes,
and my CSV is simply a comma separated list of email addresses. So, i'm
not sure how to apply the logic to make happen what i want to happen.
 
G

Gunnar Hjalmarsson

Doug said:
my CSV is simply a comma separated list of email addresses.

Just email addresses, and no other data? What the h*** are you
planning to do with those addresses?
 
G

Gunnar Hjalmarsson

Doug said:
Since you care, it is a list of emails that I maintain for a small
opt in mailing list.

I see. Yes, I do care, and for obvious reasons I'm suspicious by
default nowadays.
 
D

Doug Wells

Gunnar said:
Just email addresses, and no other data? What the h*** are you
planning to do with those addresses?

Since you care, it is a list of emails that I maintain for a small opt
in mailing list. And yes, it is addresses only.
 
D

Doug Wells

Gunnar said:
I see. Yes, I do care, and for obvious reasons I'm suspicious by
default nowadays.

As I haven't used Tie::File, what advantage does that give me over just
reading the file directly into an array?

i.e. my @array = <FILENAME>;
 
G

Gunnar Hjalmarsson

Doug said:
As I haven't used Tie::File, what advantage does that give me over just
reading the file directly into an array?

i.e. my @array = <FILENAME>;

Well, when suggesting that, I was assuming that the file consisted of
one record ("entry") per line, and that respective record contained
comma separated fields...

The difference would be that changing or deleting an array element
would instantly be reflected in the file.
 
B

Brad Baxter

Hi-
I seem to be drawing a complete blank, can anyone help? I have two CSV
files. I want to look at file one and for each entry, if that entry
appears in the second file remove it from the second file. So, in the
end, i will have a new second csv file that has the first file items
removed.

I wonder if CSV is a red herring. When you say, 'if that entry appears in
the second file', do you really mean, 'if that LINE appears ...' If so,
then you can ignore CSV altogether and just compare lines. If you can
load the entire first file into a hash, then it's relatively simple to:

#!/usr/bin/perl
use warnings;
use strict;

my %hash;
open F1, 'file1' or die $!;
open F2, 'file2' or die $!;
open F3, '>file3' or die $!;
map $hash{ $_ } = 1, <F1>;
map { print F3 unless $hash{ $_ } } <F2>;
rename 'file3', 'file2' or die $!;

__END__
file1:
abc
def
ghi

file2:
123
abc
456

file3 (new file2):
123
456


If not, then it's a different problem. :)

Regards,

Brad
 
B

Brad Baxter

my %hash;
open F1, 'file1' or die $!;
open F2, 'file2' or die $!;
open F3, '>file3' or die $!;
map $hash{ $_ } = 1, <F1>;
map { print F3 unless $hash{ $_ } } <F2>;
rename 'file3', 'file2' or die $!;

Okay, nobody called me on this, so I'm feeling guilty. I wouldn't use map
this way. Instead perhaps this:

$hash{ $_ } = 1 while <F1>;
$hash{ $_ } or print F3 while <F2>;

It isn't that I'm against using map in void context, but the above is
simply gratuitous and probably wasteful. :)

Regards,

Brad
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top