Perl - automating if statement test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - automating if statement test
# 1  
Old 12-31-2009
Perl - automating if statement test

Hello all,

I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file.

There are approximately 200 different input files. Each has the same format for each record, but the substring to check for is different for each one.

Suppose the Perl script name is test.pl. I'd like to have another file called test.txt. Each line in test.txt represents a test statement for a particular file.

For example:
testfile1.txt,\$$substrText = "string1"
testfile2.txt,\$$substrText = "string2"

The script test.pl checks the test.txt for the input file name and uses the corresponding test statement.

I tried using the eval function, but I can't get it to work. Is my design viable? Or is there a better way to do this?

Thanks for your help.
# 2  
Old 12-31-2009
I realize you are asking for perl help, but since it's been about 10 years since I last did anything more than fix a sigil in perl, perhaps this sh script will help you out.

Code:
while IFS=, read fname key; do
    fgrep "$key" "$fname" >> output
done < test.txt

Happy New Year,
alister
# 3  
Old 12-31-2009
I appreciate the suggestion, however, the script does have some other functionality. I would prefer to keep in in Perl, so I don't have to convert everything else.
# 4  
Old 12-31-2009
Create a hash of key, value pair such that
key = filename
value = pattern to be searched in the file

parse the test.txt file and create a hash as said above

and before parsing each of the 200 files, get the pattern to be searched by passing the key => filename and use the pattern returned by the hash for testing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

2. Shell Programming and Scripting

automating a perl script

Hi, I have a perl script that takes in 2 numerical values as ARGV. perl script.pl parameter1 num1 num2 in my case I have 1000's of num1 and num2. I can have them in separate files. Please let me know how to automate this run using shell scripting or using awk, so that I don't have to... (4 Replies)
Discussion started by: Lucky Ali
4 Replies

3. Shell Programming and Scripting

Help with test statement

Hello, I am trying to build a test statement but I can't make it work I want to rearrange some fields, so if my "$cfg" variable contains a string ending with .log (*.log) I want to move it in another field. Any help will be much appreciated! Thank you Shell:sh if then log="${cfg}"... (9 Replies)
Discussion started by: drbiloukos
9 Replies

4. Shell Programming and Scripting

string test in IF statement

How do I test multiple words in a string test like below: if ] then print "You entered $TBS name.\n" else print "You entered an incorrect response.\n" fi This test does not work. I have tried different syntax versions. How does this work? And is there a better way to do it? ... (10 Replies)
Discussion started by: djehresmann
10 Replies

5. Shell Programming and Scripting

Automating A Perl Script over a database

Dear Scripting Gods I've never done shell scripting before and have only recently got to grips with Perl, so apologies for my naivity. I've written a perl program which takes in two files as arguments (these are text documents which take in the information I need) The perl program spits out a... (1 Reply)
Discussion started by: fraizerangus
1 Replies

6. UNIX for Dummies Questions & Answers

if test statement

Can you use an if statement after an else? example if then echo "word" else if then echo "word" (1 Reply)
Discussion started by: skooly5
1 Replies

7. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

8. Linux

Automating build and test process

Hey ppl, I've been asked to automate the build and test process for my team at office.we work on Linux and use Perforce for SCM. I've just joined this company and dont have much knowledge on unix scripts. Could someone tell me how to go about doing this?:confused: (8 Replies)
Discussion started by: laxmi
8 Replies

9. Shell Programming and Scripting

Automating build and test process

Hey ppl, I've been asked to automate the build and test process for my team at office.we work on Linux and use Perforce for SCM. I've just joined this company and dont have much knowledge on unix scripts. Could someone tell me how to go about doing this? (0 Replies)
Discussion started by: laxmi
0 Replies

10. Shell Programming and Scripting

Automating RBAC with IF/Then statement

what would be easier to automate a script if/then ? (0 Replies)
Discussion started by: deaconf19
0 Replies
Login or Register to Ask a Question