Results 1 to 8 of 8
Hi,
I am a C++ programmer and never used Perl. I am looking for help in order to add a functionality to a script, in Perl language.
Originally, the script ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-18-2011 #1Just Joined!
- Join Date
- Mar 2011
- Posts
- 9
Adding functionality to Perl script
Hi,
I am a C++ programmer and never used Perl. I am looking for help in order to add a functionality to a script, in Perl language.
Originally, the script takes 1 file input and I run it in this way: ./script.pl file1
It is designed to return specific words from that file, example:
user
admin
printer-1
user-2
server
The idea is that I need to be able to provide the script with a second input file that contains only few words, each on a new line. These words might have a "dash". Example:
printer-2
server
user-2
I will be running the script as: ./script.pl file1 file2
In return, the script should check if every word in file1 does exist in file2. If it does exist, then the script displays it.
In the example I provided above, the script should only return:
server
user-2
Here is the script I have. What's in black is the original working Perl script and what's in red is what I would like to add in Perl Language:
!/usr/bin/env perl
my ($table);
for (every value in file2)
{
read it and put it in an array of strings
}
while (<>) {
m{Hello\s+(\S+)};
if ( defined($1) and this word exists in the array ){
$table = $1;
print "$table\n"};
}
}
exit(0);
Thank you for any assistance!
- 03-18-2011 #2
Why modify it? You can pipe the result of script.pl to another script, preserving the old functionality:
Another option would be to use awk.Code:$ script.pl file1 | grep -E 'server|user-2'
- 03-18-2011 #3Just Joined!
- Join Date
- Mar 2011
- Posts
- 9
Yep that works but sometimes file2 might have 250 words and I don't want to paste them all... I would like to have a complete script.
Thanks!
- 03-18-2011 #4
Then why not write a script that reads a file line after line and building up the regular expression?
The regex builder could be your perl script. If the regex becomes too big, you could cache it to a temporary file and use grep's -f switch.Code:$ REGEX='server|user-2'; script.pl file1 | grep -E $REGEX
- 03-21-2011 #5Just Joined!
- Join Date
- Mar 2011
- Posts
- 9
Thanks for your feedback.
Unfortunately I wasn't able to get an output. Here is what I tried:
./script.pl file.out | grep -E 'value|s-error|user|Rules'
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .
and I then I tried tried:
REGEX='value|s-error|user|Rules'; ./script.pl file.out | grep -E $REGEX
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
- 03-21-2011 #6Just Joined!
- Join Date
- Mar 2011
- Posts
- 9
Is anyone able please to provide me with an Perl code as I requested above?
Thanks
- 03-21-2011 #7Just Joined!
- Join Date
- Mar 2011
- Posts
- 9
I found out that I need to use "/usr/xpg4/bin/grep" instead of just "grep"
- 03-22-2011 #8
Strange, though I can confirm that there are different versions of grep around. I've here:
$ grep --version
GNU grep 2.6.3


Reply With Quote
