Re: How to distinguish egrep result individually?

From: Icarus Sparry (
Date: 07/06/05

Date: Wed, 06 Jul 2005 00:09:05 GMT

On Tue, 05 Jul 2005 14:37:08 -0700, cpurvis3 wrote:

> Say I have this simple egrep cmd:
> egrep -n "piano" players.txt
> Returns something like: (note that each return line will have varying
> lengths and or # of fields)
> 3428: john piano tuesday after 6pm blah blah
> 5248: diane piano blah blah monday 6pm blah blah blah blah
> ...etc.
> but I want to be able to "act" on each successful line result at some
> future point in the script.
> If I set the return result(s) to a variable, say
> RR=`egrep -n "piano" players.txt` , there's no way I can quote each
> line result so that later I could access via a "for x in $RR" cmd.
> How do I control the output of an egrep cmd so that either 1.) I can
> act on each line one at-a-time

By suitable manipulation of the IFS variable. e.g.

        # function to do something with each line, in this case split it
        # at colons, and echo the first two fields
        local OIFS="$IFS"
        set -- $1
        echo line "$1" value "$2"
# Here we run the command
R=$(egrep -n piano players.txt)
# At this point R has a number of lines in it, each which may have spaces
# in it. Set the main variables so that it is split only on newlines
set -- $R
for i
        # run something which generates output, just so we can
        # see where we have split the R variable
        # And do something with it.
        hack1 "$i"