Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #247275

Re: extract values from a string

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: extract values from a string
Date 2022-04-16 05:10 +0200
Message-ID <EcGYV-92Ek-1@gated-at.bofh.it> (permalink)
References <EcGvU-92e4-15@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sat, Apr 16, 2022 at 10:37:14AM +0800, wilson wrote:
> in shell script, how can I use regex to extract values from a string?

"Some people, when confronted with a problem, think ``I know, I'll use
regular expressions.'' Now they have two problems."

> for instance the string: "black berry 12".
> I want go get the name: black berry [String]
> the price: 12 [Int]

Ideally we would want to see more input samples.  Is the "price" always
the last input word, after the last whitespace character?  Is the "name"
always the set of words up to but not including the last chunk of
whitespace characters?

Let's assume that these guesses are true.

If the chunks of whitespace are always precisely ONE space character
apiece, then it's really easy:

string="black berry 12"
name=${string% *}
price=${string##* }

And that will work in sh (POSIX, not Bourne) or bash.  We can also allow
the whitespace to be either a space or a tab (or some other esoteric
characters) by replacing the literal space with a character class pattern:

string="black berry 12"
name=${string%[[:space:]]*}
price=${string##*[[:space:]]}

That's still POSIX sh compatible.

If the chunks might be MORE than one whitespace character apiece, then
we need something a bit uglier.  We could use bash with its extended
globs:

shopt -s extglob
string=$'black berry\t\t\t12'
price=${string##*[[:space:]]}
name=${string%%+([[:space:]])"$price"}

There are many other solutions, of course.

I also suggest <https://mywiki.wooledge.org/BashFAQ/100> as additional
reading.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

extract values from a string wilson <info@bigcount.xyz> - 2022-04-16 04:40 +0200
  Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-16 05:10 +0200
  Re: extract values from a string Nicholas Geovanis <nickgeovanis@gmail.com> - 2022-04-16 18:10 +0200
    Re: extract values from a string <tomas@tuxteam.de> - 2022-04-16 18:20 +0200
      Re: extract values from a string Nicholas Geovanis <nickgeovanis@gmail.com> - 2022-04-16 18:50 +0200
      Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-16 19:30 +0200
    Re: extract values from a string Kent West <westk@acu.edu> - 2022-04-17 04:30 +0200
      Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-17 05:00 +0200
  Re: extract values from a string David Christensen <dpchrist@holgerdanske.com> - 2022-04-16 21:30 +0200
  Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:40 +0200
    Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:40 +0200
    Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-17 02:40 +0200
      Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:50 +0200

csiph-web