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:
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!
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';"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or:
echo $templete | sed "s/^.*\(connect '[^']*'\).*/\1/"
echo $templete | sed "s/^.*\(connect '[^']*'\).*/\1/"
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.
MikeOM_DBA: Solution worked perfectly, thank you.
echo $template_line_2 | awk '{FS="connect "; A=$2; print FS substr(A,1,(match(A," |;")-1))}'