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` ; |
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. |
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. |
Quote:
|
I want it to work like grep so have chosen this method.
|
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. |
Perl does in fact have a grep function which you can use to pick items out of a list.
|
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.
|
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 |
We have used the open-close method and i want to improve the performance of my query.
Thankyou for your time. |
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 Code:
# pseudo code |
Thankyou
|
All times are GMT -5. The time now is 11:11 PM. |