bash search for a pattern within a string variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
bash search for a pattern within a string variable
Does anyone know a bash command to check for a pattern match within a string. For example, if I have a string "1 2 3 4 5". I need a function that would return true if I searched for "2 3", but false if I searched for "8 9".
Thanks
PS I tried searching this forum, but the search kept locking up, so sorry if this has been answered already.
There are built-in tests for extended regular expressions (similar to what druuna uses) or Bash patterns:
Code:
#! /bin/bash
VARIABLE="Let's test this string!"
# This is for regular expressions:
if [[ "$VARIABLE" =~ "Let's.*ring" ]]
then
echo "matched"
else
echo "nope"
fi
# And here we have Bash Patterns:
if [[ "$VARIABLE" == L*ing! ]]
then
echo "matched"
else
echo "nope"
fi
kovacsbv@leviticus:~$ cat tmp.sh
#! /bin/bash
VARIABLE="Let's test this string!"
# This is for regular expressions:
if [[ "$VARIABLE" =~ "Let's.*ring" ]]
then
echo "#1 matched"
else
echo "#1 nope"
fi
if [[ "$VARIABLE" =~ Let\'s.*ring ]]
then
echo "#2 matched"
else
echo "#2 nope"
fi
kovacsbv@leviticus:~$ ./tmp.sh
#1 nope
#2 matched
kovacsbv@leviticus:~$
Actually 'case' is much much faster than even simple matching using 'if', and
can even do pattern and glob matching:
Code:
#!/bin/sh
thisString="1 2 3 4 5"
searchString="1 2"
# if you single quote your input, you could do this
# searchString=$1
case $thisString in
# match exact string
"$searchString") echo yep, it matches exactly;;
# match start of string
"$searchString"*) echo yep, it matches at the start ;;
# match end of string
*"$searchString") echo yep, it matches at the end ;;
# searchString can be anywhere in thisString
*"$searchString"*) echo yep, it matches in the middle somewhere ;;
*) echo nope ;;
esac
Actually 'case' is much much faster than even simple matching using 'if', and
can even do pattern and glob matching:
Code:
#!/bin/sh
thisString="1 2 3 4 5"
searchString="1 2"
# if you single quote your input, you could do this
# searchString=$1
case $thisString in
# match exact string
"$searchString") echo yep, it matches exactly;;
# match start of string
"$searchString"*) echo yep, it matches at the start ;;
# match end of string
*"$searchString") echo yep, it matches at the end ;;
# searchString can be anywhere in thisString
*"$searchString"*) echo yep, it matches in the middle somewhere ;;
*) echo nope ;;
esac
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.