Gentoo Archives: gentoo-user

From: Florian Philipp <lists@×××××××××××.net>
To: Gentoo User List <gentoo-user@l.g.o>
Subject: [gentoo-user] OT: cut replacement with bash builtins
Date: Sun, 27 Feb 2011 20:04:18
Message-Id: 4D6AADAA.60807@binarywings.net
1 Hi list!
2
3 I'm currently streamlining some of my shell scripts to avoid unnecessary
4 process calls where bash itself is powerful enough.
5
6 At the moment, I want to replace stuff like this:
7 string='foo:bar:foo'
8 second_field=$(echo $string | cut -d : -f 2) # should read "bar"
9
10 My current solution is using two string operations:
11 string='foo:bar:foo'
12 # remove everything up to and including first ':'
13 second_and_following=${string#*:}
14 # remove everything from the first ':' following
15 second_field=${second_and_following%%:*}
16
17 Of course, I normally do this in a single line with a subshell but it
18 still looks cumbersome. Is there a way to do it in a single operation
19 without a temporary variable? The following does not work:
20 string='foo:bar:foo'
21 second_field=${string#:%%:*}
22
23 Thanks in advance!
24 Florian Philipp

Attachments

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

Replies

Subject Author
[gentoo-user] Re: OT: cut replacement with bash builtins hamilton <hamilton@×××××.com>
Re: [gentoo-user] OT: cut replacement with bash builtins Alex Schuster <wonko@×××××××××.org>