Ben
Ben

Reputation: 62356

How can I use sed to extract a single value in command line?

I'm trying to extract information from an environment variable in my Travis-CI config so I'm looking for a 1-line linux command that'll do this based on my regex. Here's what I'm trying:

  - MAJOR_VERSION=`sed -i 's/[v?]((([\d]+)\.[\d]+)\.[\d]+)/$1/g' <<< ${TRAVIS_TAG}`
  - MINOR_VERSION=`sed -i 's/[v?]((([\d]+)\.[\d]+)\.[\d]+)/$1.$2/g' <<< ${TRAVIS_TAG}`
  - PATCH_VERSION=`sed -i 's/[v?]((([\d]+)\.[\d]+)\.[\d]+)/$1.$2.$./g' <<< ${TRAVIS_TAG}`

It appears sed expects a file or files because I get the error sed: no input files, not a string. How can I get it to expect a string?

My objective is to take a semantic version tag (e.g. - v12.34.56 or 12.34.56 and extract the version numbers.

Upvotes: 0

Views: 161

Answers (1)

William Pursell
William Pursell

Reputation: 212208

It's not clear why you are using sed at all. If TRAVIS_TAG contains nothing but a string of the form "xx.xx.xx", then you would just read it:

IFS=. read MAJOR_VERSION MINOR_VERSION PATCH_VERSION <<< "$TRAVIS_TAG"

if you're worried about a possible leading v that you want to discard:

IFS=. read MAJOR_VERSION MINOR_VERSION PATCH_VERSION <<< "${TRAVIS_TAG#v}"

Upvotes: 1

Related Questions