Solved

sed extract string from line

Posted on 2013-06-17
4
524 Views
Last Modified: 2013-06-17
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!
0
Comment
Question by:dhite99
[X]
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
4 Comments
 
LVL 29

Accepted Solution

by:
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
sys/<connect_pwd>@TKTP21
sys/<connect_pwd>@TKTP21
==>

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
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))}'
0
 
LVL 19

Expert Comment

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

Author Comment

by:dhite99
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.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

730 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