Need help with sed statement

Kwan Lowe kwan at digitalhermit.com
Tue May 4 21:30:23 UTC 2010


On Tue, May 4, 2010 at 5:08 PM, Roberto Ragusa <mail at robertoragusa.it> wrote:

> The shell syntax is not as difficult as it looks.
> The "#" operator removes a matching part at the beginning (and we are
> matching "*/" so anything followed by a slash).
> There are two variants: "#" and "##"; the first one tries to match
> as few characters as possible; the second one as many as possible.
>
> Same thing with "%" and "%%", but they remove the end of the string.
>
> Some examples:
[snip informative shell examples]
>
> The last one achieves (in two steps) what you were asking me.
>
> Simplicity is many things. The syntax is unfamiliar until you learn it,
> then it becomes "simple". Your method is easier to learn, but not as robust.
> For example, your "basename $foo .txt" will fail if $foo contains a space.
> You need quotes around foo to avoid the problem.

Well spoken...  But I think it often comes down to not what you know
-- and clearly you understand shell syntax -- but what others around
you understand, especially if they need to support the script. In many
cases I've used bash shell arithmetic and a few times have had to rip
it out and replace it with bc, non-Gnu awk, and even perl because of
portability requirements.

In the end it may be a philosophical debate. I certainly appreciate
the examples you gave. As you imply, they are not "clever" so much as
succint, and that is a good quality for a certain class of scripts.

To your point, it's a good idea to quote variables in any case so
"basename "${FOO}" is what I should have posted.

> [rragusa at thinkpad ~]$ foo="try this.txt"; basename $foo .txt
> basename: extra operand `.txt'
> Try `basename --help' for more information.
> [rragusa at thinkpad ~]$ foo="try this.txt"; basename "$foo" .txt
> try this
>
> The shell syntax is immune to the issue because it is an internal
> manipulation of a variable, not an external command involving passing
> arguments.
> That is one manifestation of simplicity, from my point of view. :-)


More information about the users mailing list