parsing simple output help needed

Goal: for each hostname (mars, phobos, and deimos grab the value of state = in a variable so I work on it further.

I figured I'll need to use sed to find the target host name, then substitute the "linefeed and what I am assuming is a tab state =" to null and grab the state (in this case "free") into a variable. What I don't know how to do is: see the output of the command showing all non-printable characters so I can setup a grep or sed line. How is this accomplished in bash?

$ pbsnodes
mars
     state = free
     np = 4
     ntype = cluster
     status = rectime=1308479899,varattr=,jobs=0.localhost.localdomain,state=free,netload=1638547057,
gres=,loadave=2.69,ncpus=4,physmem=8195892kb,availmem=7172508kb,totmem=8195892kb,
idletime=24772,nusers=1,nsessions=5,sessions=1333 1349 1353 1388 9095,
uname=Linux mars 2.6.39-ck #1 SMP PREEMPT Sat Jun 18 14:19:01 EDT 2011 x86_64,opsys=linux
     mom_service_port = 15002
     mom_manager_port = 15003
     gpus = 2

phobos
     state = free
     np = 2
     ntype = cluster
     status = rectime=1308479933,varattr=,jobs=,state=free,netload=1085755815,
gres=,loadave=2.84,ncpus=2,physmem=4019704kb,availmem=5753552kb,totmem=6116852kb,
idletime=7324,nusers=2,nsessions=6,sessions=1565 1562 1691 1716 1737 1851,
uname=Linux phobos 2.6.37-ck #1 SMP PREEMPT Sun Apr 3 17:16:35 EDT 2011 x86_64,opsys=linux
     mom_service_port = 15002
     mom_manager_port = 15003
     gpus = 1

deimos
     state = free
     np = 2
     ntype = cluster
     status = rectime=1308479890,varattr=,jobs=2.localhost.localdomain,state=free,netload=527239670,
gres=,loadave=0.52,ncpus=2,physmem=4057808kb,availmem=3955624kb,totmem=4057808kb,
idletime=644,nusers=1,nsessions=1,sessions=865,
uname=Linux deimos 2.6.39-ck #1 SMP PREEMPT Sat Jun 11 12:36:21 EDT 2011 x86_64,opsys=linux
     mom_service_port = 15002
     mom_manager_port = 15003
     gpus = 1
egrep "^[a-z]|state" > /tmp/$$

exec 5</tmp/$$

while read HOST
do
        IFS="= " read G VAL
        echo "$HOST is $VAL"
done </tmp/$$

exec 5<&-
rm /tmp/$$

---------- Post updated at 03:30 PM ---------- Previous update was at 03:26 PM ----------

Actually, awk's a lot simpler. You can tell it the record separator is "", meaning, blank lines separate records. But whitespace still separates fields. So you should get:

$ pbsnodes | awk -v RS="" '{ print $1, $4 }'
mars free
phobos free
deimos free
$

Nice, thanks for the tip with awk!

...how can I do it my way, that is get the output to display with non-printing characters such as tabs and linefeeds? I know this can be done; I did it years ago but have no record of how!

You could do command | tr '[ \n\t]' '@_#' to turn spaces into @, newlines into _, and tabs into #.

All the variables should be available in awk as various $N, though. It's not a trick, awk's actually intended to handle field-separated data.