Perform Lookup File In Perl From One input File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perform Lookup File In Perl From One input File
# 1  
Old 08-03-2011
Perform Lookup File In Perl From One input File

Hi Perl Guru,

I'm new to Perl Programming & need some help.

I need to have some kind of similar code template so I can start to work on it.
I'm been reading some forums and did not find anything that are similar so I start to work on it. I've been struggling on how to start it.

Any help will be greatly appreciated.
Thanks,
Henry.

Below are the 2 samples input files, 1 sample output file
& pseudo code logic.

File A, input file , '~' delimiter
column heading
Code:
object_type~object_name~object_values

5~Variant~12
5~ClientCharacterSet~UTF8
5~Server~[&"$TD_DB_SERVER"]
5~Variant~8


File B, input validation file (this is for lookup file ), '~' delimiter
column heading
Code:
environment~Object_type~Object_name~object_values~match_method~message

5~Variant~12~must_match_exact~E~
5~Server~*SERVER*~must_match_pattern~E~
5~ClientCharacterSet~(UTF8, ASCII)~match_any_in_list~E~

I think I will have 2 loops

1st loop will be outer loop which read in the File A until eof

For each record in File A

I need to have a inner loop to perform a lookup against rows in the File B


Here are the logic to perform lookup.

Code:
2 lookup keys are object type & object name columns for both files
  once keys matched 
   then check match_method column from File B

    if match_method = 'must_match_exact' 
     then compare  where FileA.object_values == FileB.object_values 
        if not equal 
           then write to output file
        else next

    elsif match_method = 'must_match_pattern' 
     then compare  where FileA.object_values pattern match defined in FileB.object_values 
        if not equal 
           then write to output file
        else next
 
    elsif match_method = 'match_any_in_list' 
     then compare  where FileA.object_values match any values defined in FileB.object_values 
        if not equal 
           then write to output file
        else next

    else exit the inner loop and read the next record from File A until eof
  end

Output file layout as below
'File A ' all collumns from File A ' ==> ' File B ' then all columns from File B

So the result is the 4th row from the input file A that is failed the validation

File A '5~Variant~8 ==> File B 5~Variant~12~must_match_exact~E~

Last edited by pludi; 08-03-2011 at 05:22 AM..
# 2  
Old 08-03-2011
Well, the problem is a classic join. The first two fields combined are a key, and the next two are the validation payload or to be validated payload. You can either put the smaller set in a string addressed array / a hash map for fast lookup, or disassemble, labeling for line #, sort both files' data by key and merge them, doing all of each validation at once. You can resort the errors by line number, and who will know!

We get this question regularly in various forms. It is important to see it as a classic join problem, just as in an RDBMS. In fact, loading/applying it all to a DRBMS tool means you can solve most of it in SQL.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

2. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

3. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

4. Shell Programming and Scripting

List all file that match user input perl

Hi, I want to list all file that match user input ( specified shell wildcard) but when I compile it dont list me #!/usr/bin/perl -w print "Enter Advance Search Function: "; chomp ($func = <STDIN>); my @files = glob("$func"); foreach my $file (@files) { print "$file\n";... (1 Reply)
Discussion started by: guidely
1 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. Shell Programming and Scripting

Copy input to log file in perl

Can anyone please correct the code. I am trying to copy the user input to an output file log.txt: the script works fine but when I am trying to copy the user input error is generated. Thanks in advance. #!/usr/bin/perl $temp = "/home/log.txt"; $reply = ""; print 'Enter the code:... (1 Reply)
Discussion started by: sureshcisco
1 Replies

7. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

8. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

9. Shell Programming and Scripting

perform some checks on file using perl

hi i want check for PVCS header in file if its present then check if its in proper format or not i want to do this is in perl on windows. this is what i am doing : 1 . open file 2 . check for "PVCS information" if found then store the line no to $line var. 3 . check for "sccs" header ... (0 Replies)
Discussion started by: zedex
0 Replies

10. Shell Programming and Scripting

perl question - removing line from input file

In perl I want to do remove the top line of my input file then process the next line. I want to do something like head -1 inputfile > temp grep -v temp inputfile > newinputfile cp newinputfile inputfle is this possible in perl? (3 Replies)
Discussion started by: reggiej
3 Replies
Login or Register to Ask a Question