Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

Extract part from string in Shell

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
skahlert2010
Asked:
skahlert2010
  • 6
  • 4
  • 2
  • +1
3 Solutions
 
woolmilkporcCommented:
echo $DEFAULT_BKP_RULE | awk -F"INTERVAL:|  '{print $2}' |awk '{print $1}'
0
 
farzanjCommented:
Try
$ val=$(echo $DEFAULT_BKP_RULE | sed 's/^.*INTERVAL:\([^ ]*\) .*/\1/')
$ echo $val
9

Open in new window

0
 
woolmilkporcCommented:
Sorry, missed a quotation mark:

echo $DEFAULT_BKP_RULE | awk -F"INTERVAL:"  '{print $2}' |awk '{print $1}'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
#!/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
 
woolmilkporcCommented:
Just one awk:

echo $DEFAULT_BKP_RULE |awk -F"INTERVAL:" '{print substr($2,1,index($2," ")-1)}'
0
 
ozoCommented:
#!/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
 
skahlert2010Author Commented:
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
 
woolmilkporcCommented:
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
 
skahlert2010Author Commented:
@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
 
skahlert2010Author Commented:
@ 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
 
farzanjCommented:
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
 
woolmilkporcCommented:
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
 
skahlert2010Author Commented:
@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
 
woolmilkporcCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now