LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2014, 05:56 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
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;
}

Last edited by jags1984; 11-10-2014 at 05:57 AM.
 
Old 11-10-2014, 07:13 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 11-10-2014, 07:18 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.

Last edited by jags1984; 11-10-2014 at 07:55 AM.
 
Old 11-10-2014, 01:43 PM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by jags1984 View Post
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.
 
Old 11-11-2014, 12:52 AM   #5
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
I want it to work like grep so have chosen this method.
 
Old 11-11-2014, 01:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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.
 
Old 11-11-2014, 06:11 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Perl does in fact have a grep function which you can use to pick items out of a list.
 
Old 11-11-2014, 06:19 AM   #8
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-11-2014, 09:22 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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
 
Old 11-12-2014, 12:52 AM   #10
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
We have used the open-close method and i want to improve the performance of my query.

Thankyou for your time.

Last edited by jags1984; 11-12-2014 at 12:58 AM.
 
Old 11-12-2014, 01:45 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,522

Rep: Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436
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;
 
Old 11-12-2014, 03:46 AM   #12
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thankyou
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl 5.10 on RHEL 6.2: The "for" loop code is not executing within my Perl Script hcbj44 Linux - Newbie 5 01-25-2013 12:26 AM
Simple perl log code not working. Nintendo Programming 3 05-25-2010 10:45 PM
Perl alarm not working when reading for a socket , perl seems to be buggy alix123 Programming 1 10-05-2008 08:36 PM
Perl: getting %ENV values using su cmd chrism01 Programming 10 05-25-2007 12:11 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 04:22 PM


All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration