LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash search for a pattern within a string variable (https://www.linuxquestions.org/questions/programming-9/bash-search-for-a-pattern-within-a-string-variable-448022/)

nutthick 05-24-2006 06:17 AM

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.

druuna 05-24-2006 10:14 AM

Hi,

I don't know any bash internal that can do this, but the following code snippet is one way of solving this (there are probably a lot more):
Code:

#!/bin/bash

thisString="1 2 3 4 5"

searchString="8 9"

if `echo ${thisString} | grep "${searchString}" 1>/dev/null 2>&1`
then
  echo "True"
else
  echo "False"
fi

Hope this helps.

spirit receiver 05-24-2006 12:51 PM

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


nutthick 05-31-2006 01:27 AM

Thanks I've got it working now

Columbo2 07-06-2008 10:32 PM

Needs tweaking
 
It works more like this:

Code:

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:~$


simon.sweetman 02-16-2011 05:16 PM

To get #1 matching remove quotes around RE or set compat31

Code:

shopt -s compat31

gnashley 02-17-2011 03:53 AM

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


lastsurvivor 03-06-2012 03:54 AM

Thanks brother !

Quote:

Originally Posted by gnashley (Post 4261310)
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



OldManRiver 03-17-2015 07:26 PM

Hidden Files don't work
 
All,

I'm trying to read files and directories in a hidden directory.

EX:
Working with ThunderBird EMail client where it resides in:

/home/user/.thunderbird

The 2 things in this directory are:

DIR==> encryptedname.default
FILE=> profile.ini

I either need to pull this directory name directly or read it from the file where file contents are something like:

*********************************************************
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=encryptedname.default
*********************************************************

Anyway always get error:

Invalid file or directory

when trying to read either. Tried cat, grep, find, etc. always get the same error.

Could use some help around this roadblock!

Cheers!

OMR


All times are GMT -5. The time now is 09:25 AM.