Solved

Extract part from string in Shell

Posted on 2013-01-21
14
599 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
Industry Leaders: 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!

 
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

Industry Leaders: 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!

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

679 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