Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    how to find numbers in string


    hi, I want find numbers in string. its simple task, but I need to find numbers bigger then 9 too. my english is quite bad so I give you example for understand: from string "a20b1++q140" script find numbers x[0]=20 x[1]=1 x[2]=140. can somebody help me with that?

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Hi Giuseppe,

    it would look to me that you've got the numbers already parsed. But what do you want to do with them?

  3. #3
    thanks for answer
    no, that was only example what script would have to do, but I have problem to code script for it. sorry, I wrote it bad..

  4. $spacer_open
    $spacer_close
  5. #4
    I'd use bash built-in arrays and sed, e.g.:

    Code:
    #!/bin/bash
    declare -a array
    string="foo1bar2shite3blah4"
    array=($(echo $string|sed -e 's/[^0-9]/ /g'))
    echo Number of numbers found: ${#array[*]}
    echo "first number:  ${array[0]}"
    echo "second number: ${array[1]}"
    echo "third number:  ${array[2]}"
    echo "fourth number: ${array[3]}"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •