help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Splitting variable into two numbers


From: Greg Wooledge
Subject: Re: Splitting variable into two numbers
Date: Tue, 20 Jul 2021 14:50:30 -0400

On Tue, Jul 20, 2021 at 08:38:46PM +0200, lisa-asket@perso.be wrote:
> I could get somewhere with the following
> echo "{5,8}" | awk -F"[{}]" '{print $2}'

I categorically reject any solution that forks a whole new program just
to do some trivial string parsing.  It's nearly as bad as the old Bourne
shell days when people had to fork an expr(1) program just to add 1 to
a counter variable.

The solution I gave does not fork any new processes (neither subshells
nor whole new programs), nor does it involve creating temporary files,
setting up pipes, or any of the other expensive operations that parsing
input strings in bash often requires.

This is possible because the specification for the input format is so
strict.  There are always exactly two numbers, with known punctuation
around them.  So, POSIX parameter expansions are quite sufficient.

> Am just searching through a list of files between lines p and q.

That's what you're using the outputs for.  But where does the *input*
come from?  Why is it in this weird format with curly braces?

Obviously you're free to ignore the question, because I've already
answered your literal question.  I just can't help feeling that you're
asking the wrong question, and if you look at the bigger picture, you
could avoid some of the work you've already done and simplify a whole
lot more of your life.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]