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


Groups > linux.debian.user > #247302

Re: extract values from a string

From David Christensen <dpchrist@holgerdanske.com>
Newsgroups linux.debian.user
Subject Re: extract values from a string
Date 2022-04-16 21:30 +0200
Message-ID <EcWhj-9bFQ-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 4/16/22 09:47, Nicholas Geovanis wrote:

> On 4/15/22 19:37, wilson wrote:
>> Hello
>> 
>> in shell script, how can I use regex to extract values from a 
>> string? maybe value types transformation should be required.
>> 
>> 
>> for instance the string: "black berry 12". I want go get the name: 
>> black berry [String] the price: 12 [Int]
>> 
>> I did this in other language such as java/scala:
>> 
>> scala> val regex = """(.+)\s+(\d+)""".r val regex: 
>> scala.util.matching.Regex = (.+)\s+(\d+)
>> 
>> scala> str match { |   case regex(name,price) => (name,price.toInt)
>> | } val res2: (String, Int) = (black berry,12)
>> 
>> 
>> But sometimes for a simple task I won't want to write a java 
>> program. so what's the shell solution?


When I first read the above, my thought was "Any language can parse 
lines of input.  What are you going to do with $name and $price once you 
have them?"  E.g. think about your input data, your data structures, 
your algorithms, and your output requirements when picking a 
programming/ scripting language.


That said, platform capabilities are also important.  A script written 
to use /bin/sh and "standard system utilities" on Debian can be run on 
any Debian machine without needing to install any packages.  But, the 
same script could very well fail on BSD because GNU userland tools look 
like Unix userland tools, but they are not.  Caveat programmer.


> I didn't want to spoil the party and say this. But now my hand is 
> forced :-) You Mr Original Poster may find perl to be more congenial
> for regular expression use especially. 


+1


> You should find it on every single platform you find bash. 
> Regardless of OS.

I use Bash for an interactive shell because I like its usability 
features.  I use /bin/sh for scripts because of its portability 
(ignoring GNU vs. BSD userland issues).  When I need more power, I use Perl.


But, Perl is no longer as ubiquitous as it used to be.  Perl is not 
installed by default FreeBSD or Cygwin, for example.  Python now seems 
to be the favored super shell scripting language.  I have avoided Python 
because its syntax is whitespace dependent, but someone picking a "P" 
language to learn today should seriously consider Python.


David

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