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!

grep and spaces, how do I? 1

Status
Not open for further replies.

kd4pba

MIS
Mar 31, 2002
43
US
I am working on a shell script, and I have run into an issue with grep and spaces.

Can I do this or is there a better way?

Here is the part thats the problem

#################################################
for sat in HST ISS "STARSHINE 2" "STARSHINE 3"

do grep -A2 $sat /home/satdata >> datafile
#################################################

My problem is with the spaces in the items named STARSHINE 2 and STARSHINE 3. when they are assigned to $sat grep sees STARSHINE and 2 each as a seperate item. I have seen [:space:] , but cannot find much info on it.

Any suggestions?? Comments?? or a better way?

Thanks,

Chris
 
Use double-quotes around the variable like this:
Code:
do grep -A2 "$sat" /home/satdata >> datafile
 
That does work for the spaces (thanks) but the only issue there is that it will only pull the first item listed in the quotes (in the above for statment) and not the rest.

in otherwords instead of the output to datafile being

HST
data data data data data
data data data data data
ISS
data data data data data
data data data data data
STARSHINE 2
data data data data data
data data data data data
STARSHINE 3
data data data data data
data data data data data

It ends up as:

STARSHINE 2
data data data data data
data data data data data

and the rest is ignored. Thats actually kinda intresting

gee this is a good puzzle ;-)
 
Got it to work

My bad,

The reason it was only returning one listing instead of 2 was that I had issues in the data file it was trying to read from.

I had thought about surounding the value in quotes as you did, but I said Naa that's not it. You were correct!!

Thanks for the suggestion raider2001!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top