1

I am working on a project in which I need to make a url call to one of my server from the bash shell script..

http://hostname.domain.com:8080/beat

After hitting the above url, I will be getting the below response which I need to parse it and extract value of state from it

num_retries_allowed: 3 count: 30 count_behind: 100 state: POST_INIT num_rounds: 60 hour_col: 2 day_col: 0

Now I want to extract state variable value using regular expressions. I am able to extract count and count_behind value from it but not sure how to extract state value from it.

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for count and count_behind
COUNT=$(echo $DATA | grep -oE 'count: [0-9]+' | awk '{print $2}')
COUNT_BEHIND=$(echo $DATA | grep -oE 'count_behind: [0-9]+' | awk '{print $2}')

# how to extract state variable value here?
STATE= what do I add here?

Also if in $DATA if state variable is not there by any chance, then I want to assign 0 to STATE variable. After that I want to verify the conditionals and exit out of the script depending on that.

If STATE is equal to POST_INIT then exit successfully out of the shell script or STATE is equal to 0, then exit successfully as well.

#verify conditionals
if [[ $STATE -eq "POST_INIT" || $STATE -eq "0" ]]; then exit 0; fi

1 Answer 1

1

You can use this grep -P:

state=$(grep -oP 'state: \K\S+' <<< "$DATA")   
[[ -z "$state" ]] && state=0
echo "$state"
POST_INIT
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks anubhava. How do I assign zero value if state is missing by any chance in the $DATA?
After grep command you can just do: [[ -z "$state" ]] && state=0
Yes that's what I meant, meaning if state: string is missing in input. Can you update your answer with this as well so that it is complete?
Cool. Thanks. Can you explain little bit how this works if state: string is missing then how will it assign 0 to it and how will it work in normal case. Just for my understanding purpose.
If state: is missing in input then grep's output is empty and hence state variable is also empty. And -z "$state" is check for emptiness of variable. Part after && executed if left hand condition evaluates to true.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.