12

In bash script, what is the easy way to extract a text pattern from a string?

For example, I want to extract X followed by 2 digits in the end of the string?

user1533326
  • 161
  • 1
  • 3
  • 6
  • 1
    [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? Show us something broken, we'll tell you how to fix it. – ghoti Jul 18 '12 at 01:23
  • 1
    learn sed and regular expressions. – Thomas Dignan Jul 18 '12 at 01:26
  • I liked the `egrep` solution presented in another question https://stackoverflow.com/questions/11568859/how-to-extract-text-from-a-string-using-sed – Girardi Mar 20 '18 at 18:19

5 Answers5

30

There's a nifty =~ regex operator when you use double square brackets. Captured groups are made available in the $BASH_REMATCH array.

if [[ $STRING =~ (X[0-9]{2})$ ]]; then
    echo "matched part is ${BASH_REMATCH[1]}"
fi
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • By far the easiest solution !!! I just passed 2 hours to understand how to use Pattern Expansion, but it seems a little bit weird !!! so thank you ;) – HanniBaL90 Dec 02 '17 at 19:29
16

Lets take your input as

Input.txt

ASD123
GHG11D3456
FFSD11dfGH
FF87SD54HJ

And the pattern I want to find is "SD[digit][digit]"

Code

grep -o 'SD[0-9][0-9]' Input.txt

Output

SD12
SD11
SD54

And if you want to use this in script...then you can assign the above code in a variable/array... that's according to your need.

Debaditya
  • 2,419
  • 1
  • 27
  • 46
  • 2
    **+1** - I forgot about `grep -o`. Good suggestion, though per the OP's question I suspect it would be good to include: `grep -o 'X[0-9][0-9]$' input.txt` – ghoti Jul 18 '12 at 13:25
8
$ foo="abcX23"
$ echo "$(echo "$foo" | sed 's/.*\(X[0-9][0-9]\)$/\1/')"
X23

or

if [[ "$foo" =~ X[0-9][0-9]$ ]]; then
  echo "${foo:$((${#foo}-3))}"
fi
ghoti
  • 45,319
  • 8
  • 65
  • 104
3

You can also use parameter expansion:

V="abcX23"
PREFIX=${V%%X[0-9][0-9]} # abc
SUFFIX=${V:${#PREFIX}}   # X23
sfstewman
  • 5,589
  • 1
  • 19
  • 26
0

I need to extract the host port from this string: NIC 1 Rule(0): name = guestssh, protocol = tcp, host ip = , host port = 2222, guest ip = , guest port = 22

That string is obtained by using: vboxmanage showvminfo Mojave | grep 'host port', I mean is filtered and I need to extract whatever number be in the host port; in this case is 2222 but it can be different.

YadirHB
  • 168
  • 14
  • This was originally a question and I got the solution: if [[ "$(vboxmanage showvminfo Mojave | grep 'host port')" =~ (host\ port\ =\ [0-9]+) ]]; then echo "Port is '${BASH_REMATCH[1]/host\ port\ =\ /}'" fi – YadirHB Mar 15 '19 at 21:34