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...