Shankar Shankar -4 years ago 132
Bash Question

How to cut the string using bash command

How to cut the following string and retrieve "Not Running" Status in Bash.
I have tried cut command

cut -d"STATUS"
, It is throwing exception


$ echo "NAME(APACHE) STATUS(Not Running)

Expected String:

Not Running

Answer Source

Using the bash, regex operator ~

string="NAME(APACHE) STATUS(Not Running)"
[[ $string =~ ^.*STATUS\((.*)\)$ ]] && printf "%s\n" "${BASH_REMATCH[1]}"
Not Running

Using Awk as () de-limiter,

echo "NAME(APACHE) STATUS(Not Running)" | awk 'BEGIN{FS="[()]"}{print $4}'
Not Running

Using a double-fold parameter substitution, as bash does not support nested parameter expansions,

printf "%s\n" "${temp2#*STATUS(}"
Not Running

Using GNU grep with its PCRE capabilities, with the -P flag,

grep -oP '.*STATUS\(\K[^\)]+' <<<"$string"
Not Running
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download