Link to home
Start Free TrialLog in
Avatar of dhite99
dhite99

asked on

sed extract string from line

Hello,

I have a string in a variable from which I need to extract a substring. The substring can occur anywhere in the string, but it will only occur once.

Example:
template_line_1="allocate channel c11 device type disk maxopenfiles 1  connect 'sys/<connect_pwd>@TKTP21' format '/u902/backup/TKTP2/database/backup_%d_<static_date_seq>_%t%s%p';"
template_line_2="allocate channel c11 device type disk maxopenfiles 1 format '/u902/backup/TKTP2/database/backup_%d_<static_date_seq>_%t%s%p' connect 'sys/<connect_pwd>@TKTP21';"

Open in new window


Considering each of the two variables above, what I need to to is extract the string beginning with word " connect " (surrounded by a space) and ending with the second single quote (') after the " connect ".

The resulting extraction from both example variables would be:

connect 'sys/<connect_pwd>@TKTP21'

I received a helpful direction from a person here that directed me to use following form of sed:

echo $template_line_1 | sed "s/.*\(connect .*'\)/\1/"

which works fine as long as the connect appears at the end of the string, but that is not always the case.

Please consider the two example variables above and the resulting extracted string as the problem to solve. It does not have to be sed, but it appears that it could work with sed with some modification.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of woolmilkporc
echo $template_line_1 | awk '{FS="connect "; A=$2; print FS substr(A,1,(match(A," |;")-1))}'
echo $template_line_2 | awk '{FS="connect "; A=$2; print FS substr(A,1,(match(A," |;")-1))}'
or:
  echo $templete | sed "s/^.*\(connect '[^']*'\).*/\1/"
Avatar of dhite99
dhite99

ASKER

Woolmilkporc: The only thing I could get it to produce is the word "connect". I'm sure I'm doing something wrong though, because I have never seen you be wrong on anything.

MikeOM_DBA: Solution worked perfectly, thank you.