views:

183

answers:

3

Requirement:-

File1 has contents like -

ABCD00000001,\some\some1\ABCD00000001,Y,,5 (this indicates there are 5 file in total in unit)

File2 has contents as ABCD00000001

So what i need to do is check if ABCD00000001 from File2 exist in File1 - if yes{

print the output to Output.txt till it finds another ',Y,,X'}

else{ No keep checking}

Anyone? Any help is greatly appreciated.

Hi Arkadiy Output should be :- any filename from file 2 -ABCD00000001 in file1 and from Y to Y . for ex :- file 1 structure will be :-

ABCD00000001,\some\some1\ABCD00000001,Y,,5
ABCD00000001,\some\some1\ABCD00000002
ABCD00000001,\some\some1\ABCD00000003
ABCD00000001,\some\some1\ABCD00000004
ABCD00000001,\some\some1\ABCD00000005
ABCD00000001,\some\some1\ABCD00000006,Y,,2
so out put should contain all line between 
ABCD00000001,\some\some1\ABCD00000001,Y,,5 and

ABCD00000001,\some\some1\ABCD00000006,Y,,2

#!/usr/bin/perl -w
use strict;


my $optFile = "C:\\Documents and Settings\\rgolwalkar\\Desktop\\perl_scripts\\SampleOPT1.opt";
my $tifFile = "C:\\Documents and Settings\\rgolwalkar\\Desktop\\perl_scripts\\tif_to_stitch.txt";
print "Reading OPT file now\n";
open (OPT, $optFile);
my @opt_in_array = <OPT>;
close(OPT);

foreach(@opt_in_array){
    print();
}

print "\nReading TIF file now\n";

open (TIF, $tifFile);
my @tif_in_array = <TIF>;
close(TIF);

foreach(@tif_in_array){
    print();
}

so all it does it is reads 2 files "FYI -> I am new to programming"

+1  A: 

Try breaking up your problem into discrete steps. It seems that you need to do this (although your question is not very clear):

  • open file1 for reading
  • open file2 for reading
  • read file1, line by line:
    • for each line in file1, check if there is particular content anywhere in file2

Which part are you having difficulty with? What code have you got so far? Once you have a line in memory, you can compare it to another string using a regular expression, or perhaps a simpler form of comparison.

Ether
hmm just want help with :- how to print from Y to Y i have pasted the desired output in the main comment.
rgolwalkar
@golwalkar => simply posting desired output isn't good enough. post some sample code that you think should work. Ether's answer is a "teach a man to fish" type answer.
Eric Strom
ok doing the same in the main code .Confession :- just was able to read the files thats all.
rgolwalkar
i have added the code and yes Esther it does nothing except reading the two files :(
rgolwalkar
A: 

OK, I'll bite (partially)...

First general comments. Use strict and -w are good, but you are not checking for the results of open or explicitly stating your desired read/write mode.

The contents of your OPT file kinda sorta looks like it is CSV and the second field looks like a Windows path, true? If so, use the appropriate library from CPAN to parse CSV and verify your file names. Misery and pain can be the result otherwise...

As Ether stated earlier, you need to read the file OPT then match the field you want. If the first file is CSV, first you need to parse it without destroying your file names.

Here is a small snippet that will parse your OPT file. At this point, all it does is print the fields, but you can add logic to match to the other file easily. Just read (slurp) the entire second file into a single string and match with your chosen field from the first:

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new();
my @opt_fields;


while (<DATA>) {
    if ($csv->parse($_)) {
        push @opt_fields, [ $csv->fields() ];
    } else {
        my $err = $csv->error_input;
        print "Failed to parse line: $err";
    }
}


foreach my $ref (@opt_fields) {
#   foreach my $field (@$ref) { print "$field\n"; }
    print "The anon array: @$ref\n";
    print "Use to match?: $ref->[0]\n";
    print "File name?: $ref->[1]\n";
}

__DATA__
ABCD00000001,\some\some1\ABCD00000001,Y,,5
ABCD00000001,\some\some1\ABCD00000002
ABCD00000001,\some\some1\ABCD00000003
ABCD00000001,\some\some1\ABCD00000004
ABCD00000001,\some\some1\ABCD00000005
ABCD00000001,\some\some1\ABCD00000006,Y,,2
drewk
@Drewk - thanks a lot for helping me out on this - i am trying it now - once done will paste the code with result
rgolwalkar
A: 

$file_content_one = "text";
$file_content_two = "the text in file two";

if($file_content_two =~ /$file_content_one/)
    {
    print STDOUT "no way, file 2 contains the text of file one!\n";
    }



=~ should be read as 'contains'

at this all you need to figure out is how to read a file into a variable.
just google that, its really easy.

Hermann Ingjaldsson