Solved

sed extract string from line

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

696 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