Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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 musing on the Monastery: (5)
As of 2014-09-21 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls