Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Grepping a File

by mickeyn (Priest)
on Apr 17, 2006 at 05:28 UTC ( #543763=note: print w/ replies, xml ) Need Help??


in reply to Grepping a File

Hi,

Try this -

First, tie your file to an array and 'grep' the lines you wish into a new array.
(that's your "cat defines.h | grep 42")

Second, print the field you wish from each line.
(that's your "awk '{print $2}'")

use Tie::File; tie @array, 'Tie::File', "defines.h" || die; my @lines = grep /42/, @array; untie @array; foreach (@lines) { split; print $_[1]; # = AWK's $2 }

BTW, your awk code can be written without the use of pipes, grep & cat (and will probably work faster this way):

awk '/42/ {print $2}' defines.h

Enjoy,
Mickey


Comment on Re: Grepping a File
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://543763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2015-07-06 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls