The stupid question is the question not asked | |
PerlMonks |
Grepping a Fileby carcassonne (Pilgrim) |
on Apr 16, 2006 at 17:29 UTC ( [id://543666]=perlquestion: print w/replies, xml ) | Need Help?? |
carcassonne has asked for the wisdom of the Perl Monks concerning the following question:
Folks,
Using a C header file as source, I'd like to get the define string for a certain number. This has to be done several times, so I'd like to load the file in memory and get the result from memory in order to save the overhead associated with external system calls. For obtaining the same thing at the console prompt I use: cat defines.h | grep 42 | awk '{print $2}' This will return 'UNIVERSE_ANSWER' for instance, when the C header file has the following entry: #define UNIVERSE_ANSWER 42 Now, if I use Perl's grep on a buffer that contains the file it returns either 1 (scalar context) or the whole file (array context): my $result = grep ('/28/', $buff); my @result = grep ('/28/', $buff); I was expecting at least the line, so I could do a split on it after. One could do a loop on the buffer with a regex, but surely there must be a technique in Perl that makes this easy and moreover, optimized. Hence, this is my query. Any suggestions welcomed ! 2006-04-17 Retitled by planetscape, as per Monastery guidelines
Back to
Seekers of Perl Wisdom
|
|