jmtd → log → awk
Recently, I've been using awk
in shell scripts more and more often.
When I saw Thomas' Blog I reeled a bit
from of the make
/shell
quoted within. (Sorry Thomas! It's still a thought
provoking blog post):
DEBVERS ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[\[:digit:]]*://' -e 's/[-].*//')
DEBFLAVOR ?= $(shell dpkg-parsechangelog | grep -E ^Distribution: | cut -d" " -f2)
DEBPKGNAME ?= $(shell dpkg-parsechangelog | grep -E ^Source: | cut -d" " -f2)
DEBIAN_BRANCH ?= $(shell cat debian/gbp.conf | grep debian-branch | cut -d'=' -f2 | awk '{print $1}')
GIT_TAG ?= $(shell echo '$(VERSION)' | sed -e 's/~/_/')
I couldn't help but re-write it to be more efficient (and in the case of
DEBIAN_BRANCH
, more correct):
DEBVERS := $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')
VERSION := $(shell echo '$(DEBVERS)' | sed -e 's/^[0-9]*://' -e 's/-.*//')
DEBFLAVOR := $(shell dpkg-parsechangelog | awk '/^Distribution:/ {print $$2}')
DEBPKGNAME := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
DEBIAN_BRANCH := $(shell awk 'BEGIN{FS="[= ]+"} /debian-branch/ {print $$2}' debian/gbp.conf)
GIT_TAG := $(subst ~,_,$(VERSION))
Comments
Hi,
I don't mind others rewriting my code, though you have to realize that for such type of shell script code, I don't really care if it's written the most efficient or nice or whatever. I just care it does what I need. It will be run by me only, and I'll be the one who will see if there is a problem with it or not.
I've very often seen others not liking the "cut" utility. I never understood why. It's small, fast, and easy to understand.
Now, why at all, is my version of DEBIAN_BRANCH thing not correct? I don't get it... My version does what it is supposed to do, and always worked!
dpkg 1.17.0 will include a new dpkg-parsechengelog option that will avoid the need for grep/sed/awk completely in the general case:
dpkg-parsechangelog -S Version
http://anonscm.debian.org/gitweb/?p=dpkg/dpkg.git;a=commitdiff;h=72c2242d4783ee314d7f333419839ab42c8ad6c0
Why do you call awk code more efficient than sed code ?
Thank you !
awk 'BEGIN{FS="[= ]+"} /debian-branch/ {print $$2}' debian/gbp.conf
can be simplify in:
awk -F "[= ]+" '/debian-branch/ {print $$2}' debian/gbp.conf
and for this line I'm not sure that is important to run shell (awk be sufficient).
@Thomas: Sure, I quite agree, the most important thing to optimize for is developer time IMHO, so spending inordinate amount of time to rewrite things for minimal gain is not worthwhile. Please allow me to indulge in a bit of golf non the less.
The original DEBIAN_BRANCH will go wrong if someone has put a comment after the branch name in the gbp.conf file, on the same line.
@i5513: I'm not arguing that awk is more efficient than sed (so, my DEBVERS rewrite is not much better), but one awk instance is better than a chain of sed/grep/awk/cut.
@Guillem: I thought I remember seeing that somewhere, but couldn't find it in my local
dpkg-parsechangelog
. Thanks!@barmic: Thanks for the simplification! "shell" is needed to convince Make to run a command, although you could use backticks instead.
include /usr/share/dpkg/default.mk
Get one a bunch of useful variables, that are computed on demand and cached. From things like package info, cross-building, etc.