LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to determine if a string contains some characters (https://www.linuxquestions.org/questions/linux-general-1/how-to-determine-if-a-string-contains-some-characters-682462/)

steven.c.banks 11-10-2008 04:16 PM

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...

pixellany 11-10-2008 05:14 PM

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)

PTrenholme 11-10-2008 05:42 PM

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.


colucix 11-10-2008 06:10 PM

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.


philluder 08-27-2010 02:26 PM

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/


All times are GMT -5. The time now is 07:49 PM.