Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Grepping a File

by mickeyn (Priest)
on Apr 17, 2006 at 05:28 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-19 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found