http://www.perlmonks.org?node_id=543763


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