public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: Florian Philipp <lists@binarywings.net>
To: Gentoo User List <gentoo-user@lists.gentoo.org>
Subject: [gentoo-user] OT: cut replacement with bash builtins
Date: Sun, 27 Feb 2011 21:01:46 +0100	[thread overview]
Message-ID: <4D6AADAA.60807@binarywings.net> (raw)

[-- 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 --]

             reply	other threads:[~2011-02-27 20:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-27 20:01 Florian Philipp [this message]
2011-02-27 20:09 ` [gentoo-user] Re: OT: cut replacement with bash builtins hamilton
2011-02-27 21:50   ` Florian Philipp
2011-02-27 21:06 ` [gentoo-user] " Alex Schuster
2011-02-27 22:08   ` Florian Philipp

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=4D6AADAA.60807@binarywings.net \
    --to=lists@binarywings.net \
    --cc=gentoo-user@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