Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting grep results into Array??

Status
Not open for further replies.

itpmguy

Programmer
Apr 25, 2002
31
US
This is a Korn shell question. I want to put the results of a grep into an array. So, I did the following:

set -A newarray `grep XYXYXY testfile.txt`

But this puts each word into an array index instead putting each line into an array index. For ex. if the grep matches the following lines:

My name is XYXYXY
Your name is XYXYXY

then the array output is:

My
name
is
XYXYXY
Your
name
is
XYXYXY

I want the array to take the entire lines such that

newarray[0] = My name is XYXYXY
newarray[1] = Your name is XYXYXY

How can this be done?? Do I have to loop through each line in the file to put lines into the array??

Thanks


Cheap Computer Part Supply Buying Tips
VOIP Phone Service Guide
 

You need to assign the lines in a "loop":
Code:
#!/bin/ksh
i=0
while read lin
do
  x[$i]="$lin"
  ((i+=1))
done <testfile.txt
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Using "grep":
Code:
#!/bin/ksh
i=0
grep XYXYXY testfile.txt |\
while read lin
do
  x[$i]="$lin"
  ((i+=1))
done
[thumbsup2]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
If you're absolutely sure you have to deal with an array:
i=0
grep XYXYXY testfile.txt | while read x
do newarray[$i]="$x"; ((i+=1))
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

If you dont'care about spaces to be replaced by tab characters, this command will do the job :

Code:
set -A newarray $(grep XYXYXY testfile.txt |sed -e 's/  */\\t/g')
$echo ${newarray[0]}
My      name    is      XYXYXY
$echo ${newarray[1]}
Your    name    is      XYXYXY

Ali
 
Or you could change IFS
For example
Code:
#!/bin/ksh

IFS='
'
set -A array $(grep XYXY testfile)

Ceci n'est pas une signature
Columb Healy
 
Thanks. This worked. This will be used multiple times so I am trying to create a function to populate array and I am getting the following error:

populatearray[9]: newarray[174]=XXXX YYYYY X
-4.000000: not found

populatearray[9]: newarray[175]=ZZZZ XXYYY A -17.000000: not found

.. so on until the end of the grep results. Here's the populatearray function code:

the error is on the line ->> newarray[$i]="$lin". As mentioned before, this works as stand alone code and errs out only when used inside a function.

populatearray () {

arraynm=$1
file=$2

i=0
grep XYXYXY $file |\
while read lin
do
${arraynm}[$i]="$lin"
((i+=1))
done

}
I am calling the function as:

populate_array newarray filename

Any suggestions are greatly appreciated.

Cheap Computer Part Supply Buying Tips
VOIP Phone Service Guide
 
Have a look at the eval function:
eval ${arraynm}'[$i]'="\$lin"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top