I have the following string
JavaScript
x
Text1 Text2 v2010.0_1.3 Tue Jun 6 14:38:31 PDT 2017
I am trying to capture only v2010.0_1.3
using
JavaScript
echo "Text1 Text2 v2010.0_1.3 Tue Jun 6 14:38:31 PDT 2017" |
sed -nE 's/.*(v.*s).*/1/p'
and I get the following result v2010.0_1.3 Tue Jun 6 14:38:31 PDT
. It looks like sed is not stopping the first occurrence of the space, but at the last one. How can I capture only until the first occurence?
Advertisement
Answer
Using sed
sed’s regular expressions are “greedy” (more precisely, they are leftmost-longest matches). You need to work around that. For example:
JavaScript
$ s="Text1 Text2 v2010.0_1.3 Tue Jun 6 14:38:31 PDT 2017"
$ echo "$s" | sed -nE 's/.*(v[^[:blank:]]*).*/1/p'
v2010.0_1.3
Notes:
The expression
(v[^[:blank:]]*)
will capture as a group any string of non-blanks that begins withv
.s
is non-portable (GNU only).[[:blank:]]
will work reliably to match blanks and tabs in a unicode-safe way.
Using awk
JavaScript
$ echo "$s" | awk '/^v/' RS=' '
v2010.0_1.3
RS=' '
tells awk to treat a space as a record separator. /^v/
will print any record that begins with v
.