35

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
}
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Mint
  • 14,388
  • 30
  • 76
  • 108

9 Answers9

57

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]}"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
41
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))
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Added in the input method in the original question. (it’s input is in the forum of a string) – Mint Dec 14 '09 at 02:29
31

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

string="TestT100String"

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

Removes all non-digits.

Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
9

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

ToastStudios
  • 111
  • 1
  • 8
  • 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. – Mint May 02 '19 at 07:31
  • 1
    Bash is so terrible that there are people looking to do things like this all the time. – i30817 Dec 13 '21 at 13:00
4

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

Mic
  • 6,741
  • 3
  • 24
  • 25
1

using awk

linux$ echo -E "TestT100String" | awk '{gsub(/[^0-9]/,"")}1'
100
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

I don't know why nobody ever uses expr: it's portable and easy.

newName()
{
 #Get input from function
 newNameTXT="$1"

 if num=`expr "$newNameTXT" : '[^0-9]*\([0-9]\+\)'`; then
  echo "contains $num"
 fi
}
ephemient
  • 198,619
  • 38
  • 280
  • 391
0

Well , the Sed with the s/"pattern1"/"pattern2"/g just replaces globally all the pattern1s to pattern 2.

Besides that, sed while by default print the entire line by default . I suggest piping the instruction to a cut command and trying to extract the numbers u want :

If u are lookin only to use sed then use TRE:

sed -n 's/.*\(0-9\)\(0-9\)\(0-9\).*/\1,\2,\3/g'.

I dint try and execute the above command so just make sure the syntax is right. Hope this helped.

tomkaith13
  • 1,717
  • 4
  • 27
  • 39
-1

using just the bash shell

declare -a array
i=0
while read -r line
do
        case "$line" in
            *TestT*String* )
            while true
            do
                line=${line#*TestT}
                array[$i]=${line%%String*}
                line=${line#*String*}
                i=$((i+1))
                case "$line" in
                    *TestT*String* ) continue;;
                    *) break;;
                esac
            done
            esac
done <"file"
echo ${array[@]}
ghostdog74
  • 327,991
  • 56
  • 259
  • 343