Perl value lookup and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl value lookup and replace
# 1  
Old 11-16-2012
Perl value lookup and replace

Hi,

I have an input file and a config file which I need to lookup values in using Perl:

Input file:
Code:
ID, Value
COMP0,0
COMP1,1
COMP2,2
COMP3,3
COMP4,3
COMP5,5

Config file:
Code:
ID, Operation
COMP0,((@COMP1@ + @COMP2@ + @COMP3@) / @COMP4@) + @COMP5@

Expected output:
Code:
ID, Value
COMP0,7 # This is ((1 + 2 + 3) / 3) + 5
COMP1,1
COMP2,2
COMP3,3
COMP4,3
COMP5,5

The config file contains two columns (comma separated). The first column contains the ID and the second column the operation.
For each ID in the Input file, I need to check if it's in the config file, and if it's found then evaluate the operation in the second column by substituting all @ID@ values with the actual values of those ID's.

I have both files stored in array's, and I can combine the 2 files but I'm stuck doing the substitution here. Any help would be appreciated.

Thanks

EDIT: I think I found the solution myself:
Code:
       $operation =~ s/@([^@]*)@/$lookupvalue{$1}/g;
       print eval($operation) . "\n";


Last edited by Subbeh; 11-16-2012 at 05:44 AM.. Reason: Found solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

2. Shell Programming and Scripting

File field to replace lookup from another file

Hi All, I don't know how to fast do this field replace that need lookup from another file to form the update result:confused: I want to do it by general shell script Can anyone help to solve it ? Thanks for your kindly reply in advance. CK (0 Replies)
Discussion started by: ckwong99
0 Replies

3. Shell Programming and Scripting

Replace [|] in perl

Hi I have a file newfile.txt in perl. Its content is 2253397806-Dec-11 03.04.14.000000 PM9999901-May-12 03.28.21.000000 PM222212{|} 2905597803-Jan-12 01.24.24.000000 AMBank_CreateCustomerDET01-May-12 11.14.53.000000 AM232382{|} I have to replace by | and {|} by... (5 Replies)
Discussion started by: irudayaraj
5 Replies

4. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: devvvt168
1 Replies

5. Shell Programming and Scripting

perl - replace value

Hello, I am trying to change a value in a param file, with the Run time variable in .sh scripts using perl command , ========================================== Param file $$ID=<0> $$Country=<Country> ========================================== in the .sh script val=33 I am... (5 Replies)
Discussion started by: Shanks
5 Replies

6. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

7. Shell Programming and Scripting

Search - lookup - replace

Hi Chaps, I have a file which is like below: >cat file.dat FIRSTP SECONDP PATH="c:\ftp\dir\test.dat" USER=user1 PASS=*PASSWORD* Sub1=Dir Sub2=Test > I need to do the below: 1. Take each line, check if the line contains the word *PASSWORD* 2. If so check the USER (in this case... (1 Reply)
Discussion started by: deepakgang
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

lookup table in perl??

hi, i am very much new in perl and have this very basic question in the same:( the requirement is as below: i have an input file (txt file) in which i have fields invoice number and customer number. Now i have to take input this combination of invoice n customer number and check in a... (2 Replies)
Discussion started by: Bhups
2 Replies

10. Shell Programming and Scripting

Replace Perl Module name in all Perl scripts

I want to replace a Perl module name in all my Perl Scripts in the cgi-bin directory. How is it possible? I have the following statement in my scripts use myUtil; I want to change it to use myUtil777; Regards, Rahul (2 Replies)
Discussion started by: rahulrathod
2 Replies
Login or Register to Ask a Question