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

extract values from a string



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?

Thanks


Reply to: