To read a csv file and do field manipulation using perl script

pal2k
pal2k used Ask the Experts™
on
Hello,

  I am new to perl and need a small help in perl.

I have a csv ( comma separated file with two fields)
  contents and format of the file is below:
 
File name : input_file.csv

field1, field2
a1,b1
a2,b2
a3,b3
a4,a4
a5,a5
 
Logic: There are two fields in the csv file. I have to compare each value in field1 with each value in field2. if any value in field2 is not present in field1, then only those should be printed  to a output file called output.csv.
 
 
I tried the below code. I stuck with looping the array elements.
It would be great if anyone can help me.

code, I tried so far
 
 
#!/usr/bin/perl
$source_file = 'C:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
while ($line = <FH>)
{
  ($field1,$field2) = split ',', $line;
 
  print "$field1, $field2";
 
  if ($field1 eq $field2)
  {print "$field2";}
  }
close (FH);
 
Thanks


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lee WadwellProject Architect

Commented:
Hi pal2k,

I an not sure what you mean by "compare each value in field1 with each value in field2. if any value in field2 is not present in field1, then" ... are you after exact equality or partial match or other ... example work a treat.

However ... a subtle change to handle the end of line characters (CRLF):
#!/usr/bin/perl
$source_file = 'C:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
while ($line = )
{
  chomp($line);
  ($field1,$field2) = split ',', $line;
 
  print "$field1, $field2\n";
 
  if ($field1 eq $field2)
  {print "$field2\n";}
  }
close (FH);


lwadwell

Commented:
Adding to what Iwadwell said, to print the second field into output.csv.


.
.
$source_file = 'input_file.csv';
 
$output_file = 'output.csv';
open (OUT, ">$output_file") || die $!;
.
.
.
  {print OUT "$field2\n";}
.
.
.
close (FH);
close (OUT);

Open in new window

Author

Commented:
I am okay with outputting the results.

what I all need is from the above mentioned file
File name : input_file.csv

field1, field2
a1,b1
a2,b2
a3,b3
a4,a4
a5,a5

Let me put my question in another way. In the above file field1 and field2 are the columns in a csv file.
a1 is the first value in line1 in field1 and b1 is field2 valu in line1 respectively.  
I am looking for a matching criteria below:
By looking we can say that b1,b2,b3  are values in filed2(column) which are not found in field1(column). So I need print these not matched values of field2 in field1 to a output file. Probably I think I need help on looping foreach . Like every value in  if ($field1 ne $field2) which should be looped to get the resulting set. If I can get help in completing the code it would be great.

I have reframed the condition below from eq to ne
#!/usr/bin/perl
$source_file = 'C:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
while ($line = <FH>)
{
  chomp($line);
  ($field1,$field2) = split ',', $line;
 
  print "$field1, $field2\n";
 
  if ($field2 ne $field1)
  # need help for looping all values in field2 to chek with all values with field1
  {print "$field2\n";}
  }
close (FH);
Project Architect
Commented:
pal2k,

OK ... lets make sure I understand this right.  You want all of the field2 values that do not exist in file as a field1 value ... is this right?

Try below ... it uses a hash table to hold of the field1 values to check existance against.

lwadwell
#!/usr/bin/perl
$source_file = 'C:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
my %fld1;
while ($line = <FH>) {
  chomp($line);
  ($field1,$field2) = split ',', $line;
  $fld1{$field1};
}
close(FH);
open (FH, $source_file) || die ("Could not open $source_file!");
while ($line = <FH>) {
  chomp($line);
  ($field1,$field2) = split ',', $line;  
 
  if (!defined($fld1{$field2})) {
    print "$field2 does not exist as a field1 value\n";}
  }
close (FH);

Open in new window

Author

Commented:
oocyte,

  I have repiled in the other thread....but let me put here too

  Awesome !!!!  quick solution....
  I am very impressed.....thanks a lot...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial