0

I'm trying to use sed to edit a file within a makefile. When I edit a date, in the format xxxx-yy-zz it works fine. When I try to edit a version number with a format of x.y.z, it fails. I'm pretty certain this is because I need to escape the . in the version for the grep part of sed. I used this answer but it doesn't work, and I'm not good enough at this to figure it out (similar advice here). I can't give a working example due to use of external files, but here is the basic idea:

SHELL := /bin/bash # bash is needed for manipulation of version number
PKG_NAME=FuncMap
TODAY=$(shell date +%Y-%m-%d)
PKG_VERSION := $(shell grep -i '^version' $(PKG_NAME)/DESCRIPTION | cut -d ':' -f2 | cut -d ' ' -f2)
PKG_DATE := $(shell grep -i '^date' $(PKG_NAME)/DESCRIPTION | cut -d ':' -f2)

## Increment the z in of x.y.z

XYZ=$(subst ., , $(PKG_VERSION))
X=$(word 1, $(XYZ))
Y=$(word 2, $(XYZ))
Z=$(word 3, $(XYZ))
Z2=$$(($(Z)+1))
NEW_VERSION=$(addsuffix $(addprefix .,$(Z2)), $(addsuffix $(addprefix ., $(Y)), $(X)))
OLD_VERSION=$(echo "$(PKG_VERSION)" | sed -e 's/[]$.*[\^]/\\&/g' )

all: info update

info:
    @echo "Package: " $(PKG_NAME)
    @echo "Current/Pending version numbers: " $(PKG_VERSION) $(NEW_VERSION)
    @echo "Old date: " $(PKG_DATE)
    @echo "Today: " $(TODAY)
    @echo "OLD_VERSION: " $(OLD_VERSION)

update: $(shell find $(PKG_NAME) -name "DESCRIPTION")
    @echo "Editing DESCRIPTION to increment version"
    $(shell sed 's/$(OLD_VERSION)/$(NEW_VERSION)/' $(PKG_NAME)/DESCRIPTION > $(PKG_NAME)/TEST)
    @echo "Editing DESCRIPTION to update the date"
    $(shell sed 's/$(PKG_DATE)/$(TODAY)/' $(PKG_NAME)/DESCRIPTION > $(PKG_NAME)/TEST)

And this gives as output:

Package:  FuncMap
Current/Pending version numbers:  1.0.1000 1.0.1001
Current date:  2000-07-99
Today:  2015-07-11
OLD_VERSION: 
sed: first RE may not be empty
Editing DESCRIPTION to increment version
Editing DESCRIPTION to update the date

Obviously the sed on the version number is not working (the date is handled fine, and current/pending versions are correct, and the date is properly changed in the external file). Besides this particular problem, I'm sure a lot of this code is suboptimal - don't laugh! I don't know make nor shell scripting very well...

Community
  • 1
  • 1
Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • `OLD_VERSION` should be `PKG_VERSION` but in a form (escaping the `.` ?) that will work in the `sed` in rule `update`. Using `PKG_VERSION` in the `sed` line doesn't work correctly either. – Bryan Hanson Jul 12 '15 at 01:07
  • See http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460 – Ed Morton Jul 12 '15 at 01:38
  • @Ed Morton: I don't think the usecase described by OP actually requires escaping anything. The goal is to replace `1.0.1000` with `1.0.1001` using `sed` in the Makefile `update` target. So, this should work: `sed "s/$(PKG_VERSION)/$(NEW_VERSION)/" "$(PKG_NAME)/DESCRIPTION" > "$(PKG_NAME)/TEST"` – Eugeniu Rosca Jul 12 '15 at 01:43
  • @EugeniuRosca that could fail since the `.` will be treated as any character so if he happens to have something like `12031000` in his file it will be replaced with `1.0.1001`. He also should be using word boundaries. – Ed Morton Jul 12 '15 at 01:52
  • @EdMorton yep. my bad. you are right. – Eugeniu Rosca Jul 12 '15 at 01:54
  • @EdMorton So where does this stand? I *do* need to escape things? – Bryan Hanson Jul 12 '15 at 02:00
  • @BryanHanson yes, you do, and you need to anchor the expression. – Ed Morton Jul 12 '15 at 02:02
  • Can you post an answer I can study? Or something in the comments? Thank you. – Bryan Hanson Jul 12 '15 at 02:04
  • My first comment was a reference to another answer - study that and let us know if you have any questions. – Ed Morton Jul 12 '15 at 02:25
  • You're welcome. FWIW, to simplify things I wouldn't do this inside a makefile, I'd create a shell script and have it called from the makefile. – Ed Morton Jul 12 '15 at 02:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83083/discussion-between-bryan-hanson-and-ed-morton). – Bryan Hanson Jul 12 '15 at 22:08

1 Answers1

0

OLD_VERSION is empty, because you omitted the makefile shell function:

OLD_VERSION=$(shell echo "$(PKG_VERSION)" | sed -e 's/[]$.*[\^]/\\&/g' )
Eugeniu Rosca
  • 5,177
  • 16
  • 45