Skip to content
Advertisement

Capture a set of numbers in sed

I have the following string

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

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:

$ 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:

  1. The expression (v[^[:blank:]]*) will capture as a group any string of non-blanks that begins with v.

  2. s is non-portable (GNU only). [[:blank:]] will work reliably to match blanks and tabs in a unicode-safe way.

Using awk

$ 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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement