Re: How to distinguish egrep result individually?

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
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.

#!/bin/bash
hack1(){
        # function to do something with each line, in this case split it
        # at colons, and echo the first two fields
        local OIFS="$IFS"
        IFS=":"
        set -- $1
        IFS="$OIFS"
        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
OIFS="$IFS"
IFS="
"
set -- $R
IFS="$OIFS"
for i
do
        # run something which generates output, just so we can
        # see where we have split the R variable
        date
        # And do something with it.
        hack1 "$i"
done



Relevant Pages

  • retrieving a line from the middle of a huge file
    ... let's imagine I have a file with 1000 lines (of varying ... lengths). ... On unix command line, I ...
    (perl.beginners)
  • Re: How to extract text from number/text cell
    ... Assuming the numbers on the left are the only numbers in the string. ... Microsoft Excel MVP ... lengths of numbers and text with no symbol or visible separator? ...
    (microsoft.public.excel.worksheet.functions)
  • Re: cross correlation lag scale
    ... I tried your snippet of code but it didn't seem to work as got the ... Vectors must be the same lengths. ... and then run the script again. ...
    (comp.dsp)
  • Re: vectors of different length in for loop
    ... I need to run a loop where the outputs will be of different lengths. ... The script looks like this: ... I suspect it is because the the length of the vectors in "points" varies. ...
    (comp.soft-sys.matlab)
  • vectors of different length in for loop
    ... I need to run a loop where the outputs will be of different lengths. ... The script looks like this: ... I suspect it is because the the length of the vectors in "points" varies. ...
    (comp.soft-sys.matlab)