Narabhut
Narabhut

Reputation: 837

Print only the contents after a certain pattern match

I have a string like this:

query:schema:query_result{cell=ab}: <timestamp>

I'd like to just print the ab and assign it to a variable. How can I do this with grep/sed?

Upvotes: 1

Views: 53

Answers (2)

anubhava
anubhava

Reputation: 784918

You can also use awk:

s='query:schema:query_result{cell=ab}: <timestamp>'
awk -F '[=}]' '{print $2}' <<< "$s"
ab

To assign it to a variable:

var="$(awk -F '[=}]' '{print $2}' <<< "$s")"

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

You may try his,

$ var=$(grep -oP '=\K\w+' <<< "$str")

or

$ sed 's/.*=\(\w\+\).*/\1/' <<<"$var"
ab

Upvotes: 1

Related Questions