LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-24-2006, 07:17 AM   #1
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Rep: Reputation: 30
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.
 
Old 05-24-2006, 11:14 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 05-24-2006, 01:51 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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
 
1 members found this post helpful.
Old 05-31-2006, 02:27 AM   #4
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
Thanks I've got it working now
 
Old 07-06-2008, 11:32 PM   #5
Columbo2
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
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:~$
 
Old 02-16-2011, 06:16 PM   #6
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
To get #1 matching remove quotes around RE or set compat31

Code:
shopt -s compat31

Last edited by simon.sweetman; 02-16-2011 at 06:27 PM.
 
Old 02-17-2011, 04:53 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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
 
Old 03-06-2012, 04:54 AM   #8
lastsurvivor
LQ Newbie
 
Registered: Aug 2007
Posts: 11
Blog Entries: 1

Rep: Reputation: 0
Thanks brother !

Quote:
Originally Posted by gnashley View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Perl variable string search ugenn Programming 1 05-07-2004 09:19 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 12:57 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 05:50 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 09:19 PM


All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration