Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

sed extract string from line

Posted on 2013-06-17
Last Modified: 2013-06-17

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.

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.

Question by:dhite99
LVL 29

Accepted Solution

MikeOM_DBA earned 500 total points
ID: 39254497
Try this:

==> cat m0
cat <<! > dhite99.txt
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';"
awk '{split(substr($0,index($0,"connect")),tx,"\047");print tx[2];}' < dhite99.txt
==> ./m0

Open in new window

LVL 68

Expert Comment

ID: 39254509
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))}'
LVL 19

Expert Comment

ID: 39254549
  echo $templete | sed "s/^.*\(connect '[^']*'\).*/\1/"

Author Comment

ID: 39254577
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.

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
powershell function location 6 48
Run Secure WMI query from CentOS 5 82
How to use question mark (?) in filename with html 25 73
OS Selection Menu 1 47
This article will show, step by step, how to integrate R code into a R Sweave document
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question