简体   繁体   中英

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 (eg - v12.34.56 or 12.34.56 and extract the version numbers.

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}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM