LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-10-2008, 04:16 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
How to determine if a string contains some characters


I need an if statement for a Linux bash shell script, to determine if a string contains one or more of a given set of characters. For example:

randstring=caboosel1

I want to know if this string contains any of the following: 1 (one), l (lower case L), 0 (zero) or O (upper case O).

Thanks in advance...
 
Old 11-10-2008, 05:14 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Assume your string is in a variable "mystring"

echo $mystring|grep [abxy]

This will echo the string if any one of the four characters is present. You should be able to put this inside an if statement. Note that you cannot use the grep return code in the above construct (I'm not sure why---if you do "grep stuff filename" you get a return code of 1 (false) if nothing is found)
 
Old 11-10-2008, 05:42 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Code:
$ cat example
#!/bin/bash
for randstring in caboosel1 test Oops 'Hey there!';do
  if [ -z "$(echo $randstring | sed -n 's/\([1l0O]\)/\1/p')" ];
  then
    echo \"$randstring\" does not contain any of the target characters.
  else
    echo \"$randstring\" contains at least one of the target characters.
  fi
done
$ ./example
"caboosel1" contains at least one of the target characters.
"test" does not contain any of the target characters.
"Oops" contains at least one of the target characters.
"Hey there!" does not contain any of the target characters.
 
Old 11-10-2008, 06:10 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Since version 3 of bash you can use also the regular expression match. Following the example by PTrenholme:
Code:
$ cat example
#!/bin/bash
for randstring in caboosel1 test Oops 'Hey there!'
do
  if [[ "$randstring" =~ [1l0O] ]]
  then
    echo \"$randstring\" contains at least one of the target characters.
  else
    echo \"$randstring\" does not contain any of the target characters.
  fi
done
$ ./example
"caboosel1" contains at least one of the target characters.
"test" does not contain any of the target characters.
"Oops" contains at least one of the target characters.
"Hey there!" does not contain any of the target characters.
 
1 members found this post helpful.
Old 08-27-2010, 02:26 PM   #5
philluder
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Rep: Reputation: 0
oobash

Try oobash. It is an oo-style string library for bash 4. It has support for German umlauts. It is written in bash. Many functions are available: -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -matches, -replaceAll, -replaceFirst, -startsWith, -substring, -swapCase, -toLowerCase, -toString, -toUpperCase, -trim, and -zfill.

Look at the contains example:

[Desktop]$ String a testXccc
[Desktop]$ a.contains tX
true
[Desktop]$ a.contains XtX
false

http://sourceforge.net/projects/oobash/
 
  


Reply

Tags
match, string


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 5 07-28-2014 06:25 AM
deleting some characters from string B-Boy Programming 5 09-07-2008 11:32 AM
characters count in a string powah Programming 3 07-19-2007 02:03 PM
extracting first n characters of a string in perl ananthbv Programming 4 09-14-2004 07:25 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 05:43 PM


All times are GMT -5. The time now is 01:47 AM.

Main Menu
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