Gentoo Archives: gentoo-user

From: Florian Philipp <lists@×××××××××××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Re: OT: cut replacement with bash builtins
Date: Sun, 27 Feb 2011 21:52:16
Message-Id: 4D6AC72B.4040801@binarywings.net
In Reply to: [gentoo-user] Re: OT: cut replacement with bash builtins by hamilton
1 Am 27.02.2011 21:09, schrieb hamilton:
2 > On Sun, 27 Feb 2011 21:01:46 +0100, Florian Philipp wrote:
3 >> Hi list!
4 >>
5 >> I'm currently streamlining some of my shell scripts to avoid unnecessary
6 >> process calls where bash itself is powerful enough.
7 >
8 > My experience (take it for whatever you think it's worth) is that
9 > doing so often just makes things harder to follow and maintain.
10 > It's very unlikely that the overhead of a fork+exec is appreciably
11 > slowing your process down. Having said that (and in that vein) there
12 > is something more straightforward which may be useful:
13 >
14
15 Oh, I completely agree with you. In 90% of all cases, simple and clean
16 code is preferable. And if I really card about performance, I'd use C or
17 Perl. However, there are still such rare cases where everything works
18 fine and you see no reason to code it all again but those three forks in
19 the innermost loop just kill your performance.
20
21 In such cases, I optimize it and put the clean version in the comments.
22 That way, clarity doesn't suffer too much.
23
24 > [...]
25 >
26 >> My current solution is using two string operations:
27 >> string='foo:bar:foo'
28 >> # remove everything up to and including first ':'
29 >> second_and_following=${string#*:}
30 >> # remove everything from the first ':' following
31 >> second_field=${second_and_following%%:*}
32 >
33 > second_field = $(echo $string | awk -F: '{print $2}')
34 >
35 >
36
37 Regards,
38 Florian Philipp

Attachments

File name MIME type
signature.asc application/pgp-signature