LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl -n -e cmd not working in perl code (https://www.linuxquestions.org/questions/linux-newbie-8/perl-n-e-cmd-not-working-in-perl-code-4175524932/)

jags1984 11-10-2014 04:56 AM

perl -n -e cmd not working in perl code
 
I am searching and replacing a matching pattern in files.

I want both the matched pattern and the replacement pattern.

I found the below code working in commnad line but its not working in perl code.




Code:

@array=`perl -n -e '/Open\(([\w&\*\s]+)\,([\w&\*\s]+)\,([\w&\*\s]+)\)/ && print "$&: Open($2,$1) \n"' /home/TEST1/src/test.txt` ;

foreach my $line (@array)
{
print $line;
}


linosaurusroot 11-10-2014 06:13 AM

That's the ugliest thing in the history of computing. If you're already running a Perl script you don't want to start a new one with ``.

Open your input file, read it and match the contents.

jags1984 11-10-2014 06:18 AM

I will be traversing through directories, subdirectories etc and look for the pattern in several files in each of these directories.

I didnt want to open and close each file as it will take lot of time.


The perl -n -e command works in command line , there must be sameway it works in perl code too.

linosaurusroot 11-10-2014 12:43 PM

Quote:

Originally Posted by jags1984 (Post 5267498)
I didnt want to open and close each file as it will take lot of time.

Starting up a whole new process is way slower than opening and closing files.

jags1984 11-10-2014 11:52 PM

I want it to work like grep so have chosen this method.

grail 11-11-2014 12:54 AM

So I would have to agree with the others here. Also, have you thought of the fact that when called in that way Perl is in fact opening the file so you have saved nothing and added an additional process?

I do also believe that Perl has a built in grep which may be worth looking up.

linosaurusroot 11-11-2014 05:11 AM

Perl does in fact have a grep function which you can use to pick items out of a list.

jags1984 11-11-2014 05:19 AM

But the perl grep needs a open and close of file, and the contents has to be in list. Its too much time consuming if you are searching a pattern in several files.

grail 11-11-2014 08:22 AM

So I am confused. You have asked a question and have had several answers (all of which appear to be wrong or not suit you) ... so why bother asking if you already have decided on what you want to do?

Research how perl calls system functions and call as you prefer

jags1984 11-11-2014 11:52 PM

We have used the open-close method and i want to improve the performance of my query.

Thankyou for your time.

pan64 11-12-2014 12:45 AM

Glad to hear you want to improve it. Just do it! And how can we help you? Do you have a code?

You can start with something like this (so this is working on one single file):
Code:

# this is your original line
@array=`perl -n -e '/Open\(([\w&\*\s]+)\,([\w&\*\s]+)\,([\w&\*\s]+)\)/ && print "$&: Open($2,$1) \n"' /home/TEST1/src/test.txt` ;

You can do the same thing without backticks, and will be faster:
Code:

# pseudo code
#!perl -w
use strict;
open (FH, "filename" );
while (defined $line = <FH>) {
  if (/<your regexp>/ =~ $line) then print what you want or save that line  # <<< this line will work exactly as grep
}
close FH;


jags1984 11-12-2014 02:46 AM

Thankyou


All times are GMT -5. The time now is 11:11 PM.