From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QQdVC-0001JK-Qm for garchives@archives.gentoo.org; Sun, 29 May 2011 10:45:47 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B97F91C022; Sun, 29 May 2011 10:45:32 +0000 (UTC) Received: from mailgate-01.zdv.uni-mainz.de (mailgate-01.zdv.Uni-Mainz.DE [134.93.178.241]) by pigeon.gentoo.org (Postfix) with ESMTP id 456241C00A for ; Sun, 29 May 2011 10:44:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=cschwan@students.uni-mainz.de; q=dns/txt; s=ironport; t=1306665899; x=1338201899; h=from:to:subject:date:references:in-reply-to:mime-version: content-transfer-encoding:message-id; bh=a5mgPXpDneJfjyIDTB3xES/qVOsnQBDctIh2TUuUfTI=; b=Hw7GzpaYZs/DnnX1sq1uERrvk1LSQNZ49dGNAyD35l5IZ5YQEYXj8dKO ZuGYHHU2jZyOA4zZ3iKn+iar98qz9MoFH6uuojo+4egS6wzsvMcZ6btGb uvyFMQ9KEXaKHyCuq4rE3KdzxuP9kE4c7fXEPqqlkjTbRHItwHsdbAwWf c=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap0EAJsi4k0KXgZY/2dsb2JhbABVhEmia7cYj0iBK4NsgQcElRaKVQ Received: from e14hub-02.zdv.uni-mainz.de ([10.94.6.88]) by mailgate-01.zdv.uni-mainz.de with ESMTP; 29 May 2011 12:44:58 +0200 Received: from cschwan-laptop.localnet (178.27.162.130) by mail.uni-mainz.de (10.94.6.90) with Microsoft SMTP Server (TLS) id 14.1.289.1; Sun, 29 May 2011 12:44:57 +0200 From: Christopher Schwan To: Subject: Re: [gentoo-dev] RFC: sed script redundancy Date: Sun, 29 May 2011 12:44:46 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.37-gentoo-r4; KDE/4.6.3; x86_64; ; ) References: <20110520173922.391ea292@epia.jer-c2.orkz.net> <20110521193434.3f623fa6@epia.jer-c2.orkz.net> <20110522105043.GO24801@gentoo.org> In-Reply-To: <20110522105043.GO24801@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <201105291244.46610.cschwan@students.uni-mainz.de> X-Originating-IP: [178.27.162.130] X-Archives-Salt: X-Archives-Hash: 422a84a2f1e4ce688cd45c0e3ac66fa5 Thank you for that script. I experimented a bit with it and have a number of corrections and suggestions: - alias does not work because my_sed is not declared at this stage. I removed the whole alias line because I want to selectively enable my_sed - oargs must be an array in order to make quoting work: local oargs=( "${@}" ) - In the ewarn line ${oargs} should be changed to ${nargs[@]} (!?) - is it correct to treat -e and -f alike ? I am not sure about that, because the latter expects a file - If no "-e" is given, the first non-option argument is treated as the sed- script-expression, therefore I added hade=yes in the if-branch The new function now reads: my_sed() { local oargs=( "$@" ) local arg local nargs=() local hadi= local hade= while [[ -n $1 ]] ; do case "$1" in -i|--in-place) # ignore this flag hadi=yes ;; -e|--expression) shift nargs+=( "-e" "$1" ) hade=yes ;; -f|--file) shift nargs+=( "-f" "$1" ) hade=yes ;; -*) nargs+=( "$1" ) ;; *) if [[ -z ${hade} ]] ; then nargs+=( "-e" "$1" ) hade=yes elif [[ -z ${hadi} ]] ; then # there is no inline replacing, not much we can do break else sed "${nargs[@]}" "$1" | diff -q "$1" - > /dev/null && \ ewarn "sed ${nargs[@]} has no effect on $1" fi ;; esac shift done sed "${oargs[@]}" } As you can see, I added support for long-options. However, testing the individual sed commands remains to be done. This could be especially difficult if input is taken from stdin (e.g. in cat foo | sed "s:a:b:g"). I tested my_sed within our sage ebuild[1]. This ebuild contains 39 sed commands and I was able to spot one useless sed. [1] https://github.com/cschwan/sage-on-gentoo/blob/master/sci- mathematics/sage/sage-4.7.ebuild On Sunday 22 May 2011 12:50:43 Fabian Groffen wrote: > On 21-05-2011 19:34:34 +0200, Jeroen Roovers wrote: > > On Fri, 20 May 2011 17:56:00 +0200 > > > > Fabian Groffen wrote: > > > sed -e "" "${file}" | diff "${file}" - > > > > > > followed by the actual sed -i -e ... > > > > > > This way I didn't need to write an intermediate file. > > > > The problem there is that sed might be called just once on any one file, > > but in the tree it is often invoked with multiple scripts, so this > > simple implementation lacks a way to evaluate which sed scripts are > > useful. > > > > Also, how do I ensure the sed replacement works only on invocations > > inside the ebuild, and not, say, in portage's internals? > > (not tested, but as proof of concept) > > alias sed my_sed > my_sed() { > local oargs="${@}" > local arg > local nargs=() > local hadi= > local hade= > while [[ -n $1 ]] ; do > case "$1" in > -i) > # ignore this flag > hadi=yes > ;; > -e|-f) > shift > nargs+=( "-e$1" ) > hade=yes > ;; > -*) > nargs+=( "$1" ) > hade=yes > ;; > *) > if [[ -z ${hade} ]] ; then > nargs+=( "$1" ) > elif [[ -z ${hadi} ]] ; then > # there is no inline replacing, not much we can do > break > else > sed "${nargs[@]}" "$1" | diff -q "$1" - > /dev/null \ > && ewarn "sed ${oargs} has no effect on $1" > fi > ;; > esac > shift > done > > \sed "${oargs}" > }