From: Christopher Schwan <cschwan@students.uni-mainz.de>
To: <gentoo-dev@lists.gentoo.org>
Subject: Re: [gentoo-dev] RFC: sed script redundancy
Date: Sun, 29 May 2011 12:44:46 +0200 [thread overview]
Message-ID: <201105291244.46610.cschwan@students.uni-mainz.de> (raw)
In-Reply-To: <20110522105043.GO24801@gentoo.org>
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 <grobian@gentoo.org> wrote:
> > > sed -e "<pattern>" "${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}"
> }
next prev parent reply other threads:[~2011-05-29 10:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-20 15:39 [gentoo-dev] RFC: sed script redundancy Jeroen Roovers
2011-05-20 15:56 ` Fabian Groffen
2011-05-21 17:34 ` Jeroen Roovers
2011-05-22 10:50 ` Fabian Groffen
2011-05-29 10:44 ` Christopher Schwan [this message]
2011-05-29 11:00 ` Fabian Groffen
2011-05-29 11:49 ` Christopher Schwan
2011-10-28 1:08 ` [gentoo-dev] " Ryan Hill
2013-06-10 3:47 ` [gentoo-dev] " Jeroen Roovers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201105291244.46610.cschwan@students.uni-mainz.de \
--to=cschwan@students.uni-mainz.de \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox