0

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.

Ben
  • 60,438
  • 111
  • 314
  • 488
  • Using `-i` is a mistake in general. Using it when processing a non-regular file is a disaster. – William Pursell May 15 '17 at 15:07
  • Based on what I have read \d might not be supported by your sed. Try using [0-9] instead. egrep might be a better option than sed. Check this page to learn more: http://stackoverflow.com/questions/11568859/how-to-extract-text-from-a-string-using-sed – NepCoder May 15 '17 at 15:08
  • And to explicitly answer the question, just stop using `-i` and sed will read from stdin. – William Pursell May 15 '17 at 15:14
  • since you are extracting information from a string, http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion might be better choice (assuming bash shell or similar) .. adding sample content of string and expected output would help – Sundeep May 15 '17 at 15:17
  • @Sundeep Thanks, I'm looking into that... I've added an example – Ben May 15 '17 at 15:38

1 Answers1

1

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}"
William Pursell
  • 204,365
  • 48
  • 270
  • 300