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


Groups > linux.debian.user > #247311

Re: extract values from a string

From Ash Joubert <ash@transient.nz>
Newsgroups linux.debian.user
Subject Re: extract values from a string
Date 2022-04-17 02:40 +0200
Message-ID <Ed17j-9erU-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 16/04/2022 14:37, wilson wrote:
> 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?

Python is a convenient and scriptable solution for many text processing 
problems. You can call it from within bash:


### suppose

X="black berry 12"


### then as a python3 one-liner with -c

python3 -c "import re; m = re.search('(.+)\\s+(\d+)', '$X'); 
print((m.group(1), m.group(2)))"

# prints: ('black berry', '12')


### and you could get the output via command substitution (edit the 
print statement to get just one group; you could do this twice to get 
both items in separate bash variables)

Y=$(python3 -c "import re; m = re.search('(.+)\\s+(\d+)', '$X'); 
print((m.group(1), m.group(2)))")

echo $Y

# prints: ('black berry', '12')


### you can also inline python as a bash here-doc

python3 <<EOF
import re
m = re.search('(.+)\\s+(\d+)', '$X')
print((m.group(1), m.group(2)))
EOF

# prints: ('black berry', '12')


Note the change to your regex: '\' changed to '\\' because bash variable 
interpolation is used to obtain $X . This could be avoided if single 
quotes were used, but then you would need another way to access your 
data, such as reading it from stdin.

It depends on your workflow, but you might consider writing the whole 
thing in Python as a script with first line #!/usr/bin/python3 . Many is 
the time I rewrote a bash script in Python when I realised that I wanted 
proper types, error handling, and unit tests.

Kind regards,

-- 
Ash Joubert <ash@transient.nz>
Director
Transient Software Limited <https://transient.nz/>
New Zealand

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