John M Gant
John M Gant

Reputation: 19308

How to extract a substring matching a pattern from a Unix shell variable

I'm relatively new to Unix shell scripting. Here's my problem. I've used this script...

isql -S$server -D$database -U$userID -P$password << EOF > $test
exec MY_STORED_PROC
go
EOF

echo $test

To generate this result...

Msg 257, Level 16, State 1:
Server 'MY_SERVER', Procedure 'MY_STORED_PROC':
Implicit conversion from datatype 'VARCHAR' to 'NUMERIC' is not allowed.  Use
the CONVERT function to run this query.
(1 row affected)
(return status = 257)

Instead of echoing the isql output, I would like to extract the "257" and stick it in another variable so I can return 257 from the script. I'm thinking some kind of sed or grep command will do this, but I don't really know where to start.

Any suggestions?

Upvotes: 5

Views: 26931

Answers (2)

fmarc
fmarc

Reputation: 1726

bash can strip parts from the content of shell variables.

${parameter#pattern} returns the value of $parameter without the part at the beginning that matches pattern.

${parameter%pattern} returns the value of $parameter without the part at the end that matches pattern.

I guess there is a better way to do this, but this should work. So you could combine this into:

% strip the part before the value:
test=${test#Msg }
% strip the part after the value:
test=${test%, Level*}
echo $test

If you're interested in the (return status = xxx) part, it would be:

result=${test#*(result status = }
result=${result%)*}
echo $result

The relevant section of the bash manpage is "Parameter Expansion".

Upvotes: 14

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Here is a quick and dirty hack for you, though you should really start learning this stuff yourself:

RC=`tail -1 $test |sed 's/(return status = \([0-9]\+\))/\1/'`

Upvotes: 2

Related Questions