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

comment 1

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!

Comment by Thomas Goirand
comment 1
The same thing applies to sed: any command written with sed together with grep, cut, and various other tools can often become a more straightforward and efficient command with sed alone.
Comment by Anonymous
comment 1

Why do you call awk code more efficient than sed code ?

Thank you !

Comment by i5513
comment 1

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

Comment by barmic
comment 6

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

jmtd
comment 7

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.

Comment by Dimitri John Ledkov