public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] OT: cut replacement with bash builtins
@ 2011-02-27 20:01 Florian Philipp
  2011-02-27 20:09 ` [gentoo-user] " hamilton
  2011-02-27 21:06 ` [gentoo-user] " Alex Schuster
  0 siblings, 2 replies; 5+ messages in thread
From: Florian Philipp @ 2011-02-27 20:01 UTC (permalink / raw
  To: Gentoo User List

[-- Attachment #1: Type: text/plain, Size: 832 bytes --]

Hi list!

I'm currently streamlining some of my shell scripts to avoid unnecessary
process calls where bash itself is powerful enough.

At the moment, I want to replace stuff like this:
string='foo:bar:foo'
second_field=$(echo $string | cut -d : -f 2) # should read "bar"

My current solution is using two string operations:
string='foo:bar:foo'
# remove everything up to and including first ':'
second_and_following=${string#*:}
# remove everything from the first ':' following
second_field=${second_and_following%%:*}

Of course, I normally do this in a single line with a subshell but it
still looks cumbersome. Is there a way to do it in a single operation
without a temporary variable? The following does not work:
string='foo:bar:foo'
second_field=${string#:%%:*}

Thanks in advance!
Florian Philipp


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-02-27 22:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-27 20:01 [gentoo-user] OT: cut replacement with bash builtins Florian Philipp
2011-02-27 20:09 ` [gentoo-user] " hamilton
2011-02-27 21:50   ` Florian Philipp
2011-02-27 21:06 ` [gentoo-user] " Alex Schuster
2011-02-27 22:08   ` Florian Philipp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox