Solved

sed extract string from line

Posted on 2013-06-17
4
515 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
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now