0

I have a shell script called demo.sh that runs a command and store the output in a variable.

Here is the script:

$#!/bin/sh
a=`asterisk -rx "core show hint 101"`
echo "${a}"

The output is:

101@hints           : SIP/101               State:Unavailable    Presence:not_set         Watchers  0

I only need to get the State section, in this case Unavailable. This state is not fixed, it changes. I need to get whatever is after State: but before Presence:

How can I do this?

Manuel
  • 21
  • 3
  • 1
    Does this answer your question? [bash: shortest way to get n-th column of output](https://stackoverflow.com/questions/7315587/bash-shortest-way-to-get-n-th-column-of-output) – bishop Feb 06 '20 at 03:31
  • Write a simple regex to extract the part of string between State and Presence. Use `sed`. – KamilCuk Feb 06 '20 at 03:49
  • 1
    `a=${a#*State:}; a=${a%%Presence:*}; echo "$a"` – jhnc Feb 06 '20 at 04:53
  • Thanks for all the comments. I resolved the issue with the solution @jhnc provided. Thanks for all guys! – Manuel Feb 08 '20 at 01:31

0 Answers0