LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 06: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, 10:14 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 12: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, 01: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, 10: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, 05: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 05:27 PM.
 
Old 02-17-2011, 03:53 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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, 03: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
 
Old 03-17-2015, 07:26 PM   #9
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Rep: Reputation: 5
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
 
  


Reply



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 06:56 AM
Perl variable string search ugenn Programming 1 05-07-2004 08:19 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

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
Open Source Consulting | Domain Registration