Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about perl grep 2

Status
Not open for further replies.

lcs01

Programmer
Joined
Aug 2, 2006
Messages
182
Location
US
In unix, we can do somthing like this:

Code:
% grep string1 *.txt | grep -v string2

Can perl grep does the same? If so, what is the correct syntax? I tried to search in this forum and google on the web, but no luck.

Thank you for your help.
 
The difference between IO operations, and IO operations hidden behind a function call are infinitesimal. The only additional time involved is that which takes to install the module using cpan.

It's not the IO that worried me, or the loading of the one module. The map function being used to read the file into a data structure for grep to work with is the culprit. Map is convenient, but I am not sure how efficient it is in this case. I could be totally wrong though.

- Kevin, perl coder unexceptional!
 
Code:
perl -pe "/string1/ && !/string2/" sourcefile.txt > resultsfile.txt




- Kevin, perl coder unexceptional!
 
KevinADC said:
Code:
perl -pe "/string1/ && !/string2/" sourcefile.txt > resultsfile.txt

That's close, but -p prints every line. He would want -n.

perl --help said:
-n assume "while (<>) { ... }" loop around program
-p assume loop like -n but print line also, like sed

Code:
perl -ne "/string1/ && !/string2/ && print" sourcefile.txt > resultsfile.txt

[upsidedown]
 
KevinADC said:
It's not the IO that worried me, or the loading of the one module. The map function being used to read the file into a data structure for grep to work with is the culprit. Map is convenient, but I am not sure how efficient it is in this case. I could be totally wrong though.

Well given that map was only included because his original problem included a file glob *.txt makes the question less important. I'm fairly confident that it would not double the amount of resources in this case though. Nevertheless, the point is double mute since if the file is that large, he should be doing in place searching anyway. More lines of code sometimes is more efficient.

Shrug.
 
That's close, but -p prints every line. He would want -n.

Ahh, yes. Thanks for catching that. Have another star. [smile]

- Kevin, perl coder unexceptional!
 
Well Kevin, I personally never even think to use one-liners. I'm just a scripter and module writer myself, and never have found much use for command line perl. Nevertheless, they are certainly fun to be able to do.

However, I'm not sure how useful they are for the standard questioner on this site. I tend to believe that most of them could do with learning the basics of making a script both readable and stable, instead of going for the most obfuscated code possible.

Anyway, thanks for the insight regardless. Always nice to learn something new.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top