Return a regex match in a Bash script, instead of replacing it

90,250

Solution 1

echo "TestT100String" | sed 's/[^0-9]*\([0-9]\+\).*/\1/'

echo "TestT100String" | grep -o  '[0-9]\+'

The method you use to put the results in an array depends somewhat on how the actual data is being retrieved. There's not enough information in your question to be able to guide you well. However, here is one method:

index=0
while read -r line
do
    array[index++]=$(echo "$line" | grep -o  '[0-9]\+')
done < filename

Here's another way:

array=($(grep -o '[0-9]\+' filename))

Solution 2

You could do this purely in bash using the double square bracket [[ ]] test operator, which stores results in an array called BASH_REMATCH:

[[ "TestT100String" =~ ([0-9]+) ]] && echo "${BASH_REMATCH[1]}"

Solution 3

Pure Bash. Use parameter substitution (no external processes and pipes):

string="TestT100String"

echo ${string//[^[:digit:]]/}

Removes all non-digits.

Solution 4

I Know this is an old topic but I came her along same searches and found another great possibility apply a regex on a String/Variable using grep:

# Simple
$(echo "TestT100String" | grep -Po "[0-9]{3}")
# More complex using lookaround
$(echo "TestT100String" | grep -Po "(?i)TestT\K[0-9]{3}(?=String)")

With using lookaround capabilities search expressions can be extended for better matching. Where (?i) indicates the Pattern before the searched Pattern (lookahead), \K indicates the actual search pattern and (?=) contains the pattern after the search (lookbehind).

https://www.regular-expressions.info/lookaround.html

The given example matches the same as the PCRE regex TestT([0-9]{3})String

Solution 5

Use grep. Sed is an editor. If you only want to match a regexp, grep is more than sufficient.

Share:
90,250
Mint
Author by

Mint

I use a Mac at home, a remote Debian server. I Dual boot Win 7 for the occasional game. I also run VMs of several other Operating Systems (Debian, Ubuntu, CentOS, Win 7, Win XP). I'm learning HTML, CSS, PHP, MYSQL, Javascript, BASH Scripting. I have little experience with C#, Perl, Python. I want to learn Python and C (or C++) one day.

Updated on April 28, 2020

Comments

  • Mint
    Mint about 4 years

    I just want to match some text in a Bash script. I've tried using sed but I can't seem to make it just output the match instead of replacing it with something.

    echo -E "TestT100String" | sed 's/[0-9]+/dontReplace/g'
    

    Which will output TestTdontReplaceString.

    Which isn't what I want, I want it to output 100.

    Ideally, it would put all the matches in an array.

    edit:

    Text input is coming in as a string:

    newName()
    {
     #Get input from function
     newNameTXT="$1"
    
     if [[ $newNameTXT ]]; then
     #Use code that im working on now, using the $newNameTXT string.
    
     fi
    }
    
  • OMG Ponies
    OMG Ponies over 14 years
    SED doesn't write to the source file - only way for that to happen is if you write the output with the same filename in order to overwrite the file.
  • Mic
    Mic over 14 years
    True, but sed is still an editor. Its name is even short for stream editor. If he only needs to match he can use grep.
  • Mint
    Mint over 14 years
    Added in the input method in the original question. (it’s input is in the forum of a string)
  • ghostdog74
    ghostdog74 over 14 years
    i don't think he wants to match. He says he needs the 100
  • ghostdog74
    ghostdog74 over 14 years
    so using grep's -o should do the job
  • Mint
    Mint over 14 years
    Yep, I need the 100, working solution found though, thanks to your guys (especially Dennis)
  • James Andino
    James Andino over 11 years
    it takes a real monkey to figure this out
  • Jake Biesinger
    Jake Biesinger over 10 years
    But see stackoverflow.com/questions/218156/bash-regex-with-quotes for issues with bash quoting in different BASH versions.
  • Yuval
    Yuval over 7 years
    I agree, only it should be said: SED is more than sufficient, while grep IS sufficient :)
  • Mint
    Mint about 5 years
    Wow, can't believe people still come here, after 10 years. I would never have guessed it would be viewed this much when I was asking it. I thought I was the only one at the time, who didn't know how to do it.
  • i30817
    i30817 over 2 years
    Bash is so terrible that there are people looking to do things like this all the time.