From: Alex Schuster <wonko@wonkology.org>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] OT: cut replacement with bash builtins
Date: Sun, 27 Feb 2011 22:06:10 +0100 [thread overview]
Message-ID: <4D6ABCC2.3050109@wonkology.org> (raw)
In-Reply-To: <4D6AADAA.60807@binarywings.net>
Florian Philipp writes:
> 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%%:*}
That's how I do these things, too.
> Of course, I normally do this in a single line with a subshell but it
Hmm, I don't get this. Subshell?
> 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#:%%:*}
I don't think so. But you can write a shell function for this:
getfield()
{
local str=${1#*:}
echo "${str%%:*}
}
string='foo:bar:foo'
second_field=$( getfield "$string" )
But if you need to do this very often in a loop, sometimes going back to
cut can speed things up, when you place it outside:
See
for string in $( < inputfile )
do
second_field=$( getfield "$string" )
do_something_with $second_field
done
vs.
secondfields=( $( cut -d : -f 2 inputfile ) )
for secondfield in ${secondfields[@]}
do
do_something_with $second_field
done
So, in th e2nd example, cut is called only once, but processes all input
lines. The result is put into an array.
Of course, all stuff should be put into double quotes in case there is
whitescape involved. Setting IFS to $'\n' may be necessary for this when
creating the array.
Wonko
next prev parent reply other threads:[~2011-02-27 21:08 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Alex Schuster [this message]
2011-02-27 22:08 ` [gentoo-user] " 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=4D6ABCC2.3050109@wonkology.org \
--to=wonko@wonkology.org \
--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