Solved

Korn Shell - How to Find A String within a String

Posted on 2013-06-05
2
395 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

18 Experts available now in Live!

Get 1:1 Help Now