Get information like substring

I retrieved values from postgresql database
return the following line

name ------------ myname (1 row)

how can I extract "myname" out of the line?

thanks

What do you really want to extract? If all you wnat is "mysname":

echo myname

If you mean the third field, it depends on where you are getting it from. If it's in a variable::

set -f
set -- $variable
echo "$3"

If you are processing multi-line output from a command:

your_command | cut -d ' ' -f3

thanks for your reply

I stored the result in a variable

name=$(pgsql -c "select name from teacher")

$name is a single line
and equal to "name ------------ myname (1 row)"
I want to get the "myname" inside this variable

e.g.
If $name is "name ------------ Peter (1 row)"
I want to get "Peter" from the line

could you give me some suggestions? thanks

echo Peter

Use the code I posted for when you have it in a variable.

set -f
set -- $name
echo $3

I used the code and it work fine, thank you very much!!:b:

The result from the sql now is a little bit different,

"name ------------ Peter (1 row)"
"name ------------ Mary Lee (1 row)"

The result is vary in word length.

set -f
set -- $name
echo $3

'Peter' is ok
'Mary' is not ok, I want to get 'Mary Chan'.

I want to get
'Peter'
'Mary Chan'

Any enhancement i could do?

name=${name% (*}
echo "${name#*- }"

I follow your suggestion

name=$(pgsql -c "select name from teacher")
name=${name% (}
echo "${name#
- }"

I got
"name"
"------------"
" Mary Lee "
"(1 row)"

Any ideas? Thanks again!!

Then you misinformed us. The result from pgsql was not a single line; each field is on a separate line.

And did you really get those quotes in the output? If not, why are they there? If you did, why didn't you show them in your previous posts?

LF='
'
name=$(pgsql -c "select name from teacher")
name=${name%$LF(*}
echo "${name#*-$LF}"

Really sorry about the misleading. Since I am not familiar with shell script, I can just tell you what I can see from the screen.

  1. Single line
    before asking

name=$(pgsql -c "select name from teacher")
echo $name

I can see only a single line display out there.
name ------------ Mary Lee (1 row)

Now I know that it is an array.
name
------------
Mary
Lee
(1
row)

There are no white spaces.

  1. "quote"

Because from the screen of
name=$(pgsql -c "select name from teacher")
name=${name% (}
echo "${name#
- }"

I found some white spaces next to Mary Lee, so want to show you there are some white spaces only.

It should be like that
<<white space>>name
------------
<<white space>>Mary Lee
(1 row)

  1. Latest code

LF='
'
name=$(pgsql -c "select name from teacher")
name=${name%$LF(}
echo "${name#
-$LF}"

------------
<<white space>>Mary Lee
<<blank line>>

Sorry again for the misinformation.

Try:

echo "$name"

No, it's not. It is a scalar variable containing more than one line.

What command produced that?

What is the output from:

pgsql -c "select name from teacher" | od -c

<<white space>>name
-----------
<<white space>>Mary Lee
(1 row)

for n in $name
do
echo $n
done

0000000 n a m e \n - - - - -
0000020 - - - - - \n M a r y L e e \n
0000040 ( 1 r o w ) \n \n
0000051

Thanks!