Solved

Korn Shell - How to Find A String within a String

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

760 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