Solved

Extract part from string in Shell

Posted on 2013-01-21
14
590 Views
Last Modified: 2013-01-21
I have the following variable: DEFAULT_BKP_RULE="CYCLIC:Y INTERVAL:9 RET_PERIOD:20 KEEP_ON_FILER:8 ACTIVE:N"

The question is how can I extract the 9 for column INTERVAL?

I tried
echo $DEFAULT_BKP_RULE | grep 'INTERVAL' | awk -F= '{print $0}' | awk '{print $2}'

Open in new window



which returns INTERVAL:9

Only the numeric value is needed though. I wouldn't like to rely on substrings since the content respectively order of $DEFAULT_BKP_RULE might change.

It's probably really easy for you! So pleas don't hesitate to suggest something.
I am looking forward to learn!
0
Comment
Question by:skahlert2010
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38801150
echo $DEFAULT_BKP_RULE | awk -F"INTERVAL:|  '{print $2}' |awk '{print $1}'
0
 
LVL 31

Accepted Solution

by:
farzanj earned 250 total points
ID: 38801151
Try
$ val=$(echo $DEFAULT_BKP_RULE | sed 's/^.*INTERVAL:\([^ ]*\) .*/\1/')
$ echo $val
9

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38801160
Sorry, missed a quotation mark:

echo $DEFAULT_BKP_RULE | awk -F"INTERVAL:"  '{print $2}' |awk '{print $1}'
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 38801173
#!/bin/bash
DEFAULT_BKP_RULE="CYCLIC:Y INTERVAL:9 RET_PERIOD:20 KEEP_ON_FILER:8 ACTIVE:N"
DEFAULT_BKP_RULE=${DEFAULT_BKP_RULE##*INTERVAL:}
echo ${DEFAULT_BKP_RULE%% *}
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 125 total points
ID: 38801180
Just one awk:

echo $DEFAULT_BKP_RULE |awk -F"INTERVAL:" '{print substr($2,1,index($2," ")-1)}'
0
 
LVL 84

Expert Comment

by:ozo
ID: 38801256
#!/bin/bash
DEFAULT_BKP_RULE="CYCLIC:Y INTERVAL:9 RET_PERIOD:20 KEEP_ON_FILER:8 ACTIVE:N"
shopt -s extglob
echo ${DEFAULT_BKP_RULE//@(*INTERVAL:| *)}
0
 

Author Comment

by:skahlert2010
ID: 38801338
Hey guys! Thanks for your answers! Amazing how many ways there are and how fast you work out such things.

@woolmilkpork: Your code returns the following in my session:

C:Y instead of 9

Can you please review it and advise show what the syntax should be like?

Thanks!
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38801376
Which of my versions do you mean?

The very first one is missing a quotation mark (as I wrote later) and doesn't work at all.

The other two (in #20054184 and #38801180) are tested with the variable you posted and work for me.

Could you please post the exact command giving this wrong "C:Y" ouput (including the content of the variable you used)?
0
 

Author Comment

by:skahlert2010
ID: 38801418
@woolmilkpork

Sorry to bug you but this is exactly what I pasted and received. Just confirmed it.

$  DEFAULT_BKP_RULE="CYCLIC:Y INTERVAL:9 RET_PERIOD:20 KEEP_ON_FILER:8 ACTIVE:N"

$ echo $DEFAULT_BKP_RULE
CYCLIC:Y INTERVAL:9 RET_PERIOD:20 KEEP_ON_FILER:8 ACTIVE:N

$ echo $DEFAULT_BKP_RULE |awk -F"INTERVAL:" '{print substr($2,1,index($2," ")-1)}'

C:Y

Open in new window

0
 

Author Comment

by:skahlert2010
ID: 38801457
@ Farzanji:

What if I wanted to search for the value of ACTIVE or some other part at the end of the string? What should be substituted then? Just trying to discover what is happening in your regex.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38801482
This should do it:

val=$(echo $DEFAULT_BKP_RULE | sed 's/^.*ACTIVE:\([^ ]*\).*/\1/')


So, I have removed one space also since active value doesn't have a space after it.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38801518
skahlert2010,

you see me quite clueless right now.

I just ran exactly what you posted and all I get is: "9", as expected.

I tried it under Linux and AIX, under both OSes with bash and ksh, always the same correct result "9".

No idea.
0
 

Author Comment

by:skahlert2010
ID: 38801562
@woolmilkpork

No worries. I believe that your code usually works. I am running Solaris 5.10 with KSH.

Thanks anyway for the effort.

@Farzanji

Thank you! It's working flawlessly! Great!

@Ozo

Your solution works as well. Very interesting approach!

Thanks to you all!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38801630
You should have told us that it's Solaris.

"awk" under Solaris accepts just one character as the field separator ("-F")
so -F"INTERVAL" was seen as -F"I".
Thus the result was the string following the first "I", starting at "1", and the length of this string was determined by the position of the first space (4) minus one (=3).

C:Y

is quite correct under these circumstances.

Thanks for the points!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now