Skip to content
Advertisement

Capture a set of numbers in sed

I have the following string

JavaScript

I am trying to capture only v2010.0_1.3 using

JavaScript

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

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

JavaScript

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