Reputation: 7397
I pipe the following string "version": "1.0.0",
to sed
and I'm trying to get 1.0.0
out of it. I want to get it by the rule: get string between : "
and ",
.
I've been trying the following:
sed -e '/:\ \"/,/\ ",/p'
sed -e 's/:\ \"\(.*\)\",/\1/'
The first one does nothing and the last one returns "version"1.0.0
, which is close, but no cigar. What am I missing?
Upvotes: 1
Views: 377
Reputation: 15461
Another approach, using grep with lookahead/lookbehind:
$ grep -oP '(?<=: ")[^"]*(?=")' <<< '"version": "1.0.0",'
1.0.0
or shorter, using \K
to exclude : "
from the matched string:
$ grep -oP ': "\K[^"]+' <<< '"version": "1.0.0",'
1.0.0
Upvotes: 1
Reputation: 785008
You need to match complete input by using .*
on either side of your search pattern to be able to replace full line with just the captured group's back-reference.
This sed
should work:
s='"version": "1.0.0",'
sed 's/.*: "\([^"]*\)",.*/\1/' <<< "$s"
1.0.0
Or even this one:
sed 's/.*: "\(.*\)",.*/\1/' <<< "$s"
1.0.0
Upvotes: 1