Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9

    Thumbs up 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!

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Why modify it? You can pipe the result of script.pl to another script, preserving the old functionality:

    Code:
    $ script.pl file1 | grep -E 'server|user-2'
    Another option would be to use awk.

  3. #3
    Just 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!

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Then why not write a script that reads a file line after line and building up the regular expression?

    Code:
    $ REGEX='server|user-2'; script.pl file1 | grep -E $REGEX
    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.

  5. #5
    Just 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 . . .

  6. #6
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9
    Is anyone able please to provide me with an Perl code as I requested above?

    Thanks

  7. #7
    Just Joined!
    Join Date
    Mar 2011
    Posts
    9
    I found out that I need to use "/usr/xpg4/bin/grep" instead of just "grep"

  8. #8
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Strange, though I can confirm that there are different versions of grep around. I've here:

    $ grep --version
    GNU grep 2.6.3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •