[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Korn Shell - How to Find A String within a String

Posted on 2013-06-05
2
Medium Priority
?
406 Views
Last Modified: 2013-06-05
Let's say a variable contains this string:
testvar="allocate channel c1 device type disk rate 20M maxopenfiles 1 format '/u900/backup/<oracle_sid>/database/backup_%d_<static_date_seq>_%t%s%p';"

Open in new window

What I need to do is:
1. preserve everything up to the word disk, then replace the word disk with 'SBT_TAPE'
2. preserve everything after the word disk up until the word "format"
3. create a new string that, considering the testvar contents above, would look like the following:
allocate channel c1 device type 'SBT_TAPE' rate 20M maxopenfiles 1  PARMS 'BLKSIZE=1048576, SBT_LIBRARY=<oracle_home>/lib/libddobk.so, ENV=(STORAGE_UNIT=emergo,BACKUP_HOST=USTSSSNSBRE024.sabre.com,ORACLE_HOME=<oracle_home>)' format 'df_%d_%I_<static_date_seq>_%U_%t%s%p';

Open in new window


Do not be concerned with the string beginning with PARMS, I can supply that no problem. The real objectives are items 1 and 2 - extract into a variable all up until the word 'disk' and then extract into another variable all after the word 'disk' and before the word 'format'.

Unfortunately I cannot just create a new line from scratch because there are thousands of these that will have to be converted and there are different options being used in the statement.

Any help will be much appreciated - thanks!
0
Comment
Question by:dhite99
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39223084
I don't know if I really understand....

1. extract into a variable all up until the word 'disk', replace 'disk' with 'SBT_TAPE':

VAR1=$(echo $testvar | sed "s/\(^.*\)disk.*/\1SBT_TAPE/")

echo $VAR1

2. extract into another variable all after the word 'disk' and before the word 'format'.

VAR2=$(echo $testvar |sed "s/.*disk\(.*\)format.*/\1/")

echo $VAR2


... or for VAR2 rather this:

VAR2=$(echo $testvar |sed "s@.*disk\(.*\)format.*@\1PARMS 'BLKSIZE=1048576, SBT_LIBRARY=<oracle_home>/lib/libddobk.so, ENV=(STORAGE_UNIT=emergo,BACKUP_HOST=USTSSSNSBRE024.sabre.com,ORACLE_HOME=<oracle_home>)'@")

echo $VAR2

Open in new window

0
 

Author Closing Comment

by:dhite99
ID: 39223132
woolmilkporc you always come through with the great answers! Thanks!!!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 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