Solved

Extract part from string in Shell

Posted on 2013-01-21
14
595 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

791 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