4

In a bash (version 3.2.48) script I get a string that can be something like:

'XY'
' Y'
'YY'
etc

So, I have either an alphabetic character OR a space (first slot), then the relevant character (second slot). I tried some variation (without grep, sed, ...) like:

if [[ $string =~ ([[:space]]{1}|[[:alpha:]]{1})M ]]; then

and

if [[ $string =~ (\s{1}|.{1})M ]]; then

but my solutions did not always work correctly (matching correctly every combination).

5
  • 1
    Please explain better what you need. Do you want to test if the string has a particular format (if so, which format)? Do you want to extract the "relevant character" from the string? Something else? Commented Aug 4, 2012 at 21:51
  • show examples - failed matches. Commented Aug 4, 2012 at 21:53
  • I need to match the git status --porcelain output. Can be something like: 'A ', 'AD', 'MM', "AM', ' M' etc. I am interested in cases where I have a 'M' as second character. Commented Aug 4, 2012 at 21:56
  • so the rule is "has either a space or a char" and them "one char at the end". and you want to get that last char? Commented Aug 4, 2012 at 21:57
  • Right. The @patrix solutions seems to achieve the desired result, thank you. Commented Aug 4, 2012 at 22:03

3 Answers 3

4

This should work for you:

if [[ $string =~ [[:space:][:alpha:]]M ]]; then
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but this one has the same issue I previously found: does not catch 'MM'. I do not know the reason.
@Max: I apologize. I omitted a colon (and you did, too). Please see my revised answer.
you are absolutely right, I forgot a colon in my code, thank you. The odd part is: the only version working in my bash is the @patrix 's (first) one: if [[ ${string:1:1} == "M" ]]; then
3
if [[ ${string:1:1} == "M" ]]; then
    echo Heureka
fi

or (if you want to do it with patterns)

if [[ $string =~ ([[:space:]]|[[:alpha:]])M ]]; then
    echo Heureka
fi

or (even simpler)

if [[ $string == ?M ]]; then
    echo Heureka
fi

7 Comments

I've added a version with patterns as well. If your problem is solved, please accept the answer
Works for me with 3.2.48, are you retyping or using copy/paste?
Tried typing and pasting. The only version working is the ${string:1:1} == "M" one...
Strange. Maybe some of bash's options are different than mine.
Could be the Shopt Builtin? There is a way to enable options for a script?
|
2

Without using regular expressions, simply pattern matching is sufficient:

if [[ $string == [[::upper:]\ ]M ]]; then
  echo match
fi

Given your example, you want [[:upper:]] rather than merely [[:alpha:]]

1 Comment

Thank you @glenn jackman : good point. Unluckily even this one does not works in my bash. The only solution giving me the exact result is ${string:1:1} == "M" . I guess there is something I am missing about that...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.