Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extract part from string in Shell

Posted on 2013-01-21
14
Medium Priority
?
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 500 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

722 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