LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What's the easiest way to match a character in a string (https://www.linuxquestions.org/questions/linux-newbie-8/whats-the-easiest-way-to-match-a-character-in-a-string-914179/)

ted_chou12 11-18-2011 12:49 AM

What's the easiest way to match a character in a string
 
Hi, I wish to make an if statement matching a letter "D" in a string with no at most 3 characters:
Code:

$string="AHD"
if [ D in $string ]; then
    ...
fi

Thanks,
Ted

davemguru 11-18-2011 01:29 AM

Quote:

Originally Posted by ted_chou12 (Post 4527240)
Hi, I wish to make an if statement matching a letter "D" in a string with no at most 3 characters:
Code:

$string="AHD"
if [ D in $string ]; then
    ...
fi

Thanks,
Ted

You want the equivalent of what other languages call the function "instr" (and 2 I can think of call "POS")
The instr function returns the position of the search string within the main string.. But, given that it returns zero if the substring is not present - then that will give the answer you want.

So, in SHELL PROGRAMMING the easiest way I know - regardless of the length of the string is;
Code:

string="AHD"
if [ `echo "$string"|grep -s "D"` ]; then
 ...
fi

Some people prefer "sed", others might use clever manipulation shell parameter expansion. But, most people "know" grep searches for stuff and that echo just echo's stuff and that the pipe connecting the two is saying "echo this and then search it".
So, for that reason - not elegance - it is the simplest and most readable IMHO.
Dave

MTK358 11-18-2011 06:54 AM

Code:

if [[ $string = *D* ]]
then
    # the string contains "D"
fi


colucix 11-18-2011 06:57 AM

Or
Code:

if [[ $string =~ D ]]
then
  echo D found in string
fi


David the H. 11-18-2011 10:50 AM

One more, just for fun:

Code:

if [[ ${string//[^D]} ]]; then
        echo "D found in string"
fi

string manipulation

Note that this is all assuming bash or similar shell. The OP didn't clearly state what language environment he's working in.

Code:

$string="AHD"
This is the wrong syntax for a shell script. You don't precede the declaration of a variable with $, only the expansion of it.

Edit in response to davemguru...

You don't even need the test brackets. if checks the exit status of the final command run, so you can use grep alone:

Code:

if grep -q "D" <<<"$string" ;then

if echo "$string" | grep -q "D" ; then

if echo "$string" | grep "D" >/dev/null ; then

If your version of grep supports the -q option you can use one of the first two options. The first one uses a bash here string rather than a pipe to avoid opening up a subshell.

On systems without -q, use a redirection to dump the output away.

davemguru 11-19-2011 06:56 AM

In response to "David the H"
Yes, unfortunately I am showing my age. My solution was based on the lowest common denominator - viz: bourne shell. :)

David the H. 11-20-2011 12:33 AM

Yeah, I recognized your intent. I just commented because it's often good to give alternatives for different conditions, and to point out that test is not the only command you can use in most branching/looping constructs.

OTOH, I just noticed that you included the -s option in your command, which the gnu grep manpage states behaved like the -q option in some implementations. It also goes on to point out that if you truly need it to be portable, use redirection instead of either option.


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