5

I am trying to modify a bash script. The script current contains

print "<div class=\"title\">" $1 "</div>"

Where $1 may look like:

Apprentice Historian (Level 1)
Historian (Level 4)
Master Historian (Level 7)

What i'd like to do is add an image which is named the "base" value. I had something like this in mind:

print "<div class=\"icon\"><imgsrc=\"icons\" $1 ".png\"></div><div class=\"title\">" $1 "</div>"

However, in this case I'd like $1 to only return Historian. I was thinking I could use a regex to match and on $1 and keep only the part I need.

(Apprentice|Master)?\s(.*)\s(\(Level \d\)) 

I know my regex isn't quite there, ideally apprentice/master would be in their own match group and not tied the base. And I don't know how to match on the $1 argument.

3
  • 5
    The pony he comes... Commented Feb 15, 2012 at 7:09
  • 1
    Thanks for the link but I'm not actually parsing [x]html. I'm actually creating an html output based on a few keywords. Commented Feb 15, 2012 at 7:13
  • @JackManey I think the OP asks to regex the Apprentice Historian (Level 1) kind of string using bash. Commented Feb 15, 2012 at 7:13

3 Answers 3

9

Using regex matching in bash:

for a in 'Apprentice Historian (Level 1)' 'Historian (Level 4)' 'Master Historian (Level 7)' ; do
    set "$a"
    echo " === $1 ==="
    [[ $1 =~ (Apprentice|Master)?' '?(.*)' ('Level' '[0-9]+')' ]] \
        && echo ${BASH_REMATCH[${#BASH_REMATCH[@]}-1]}
done 

The tricky part is to retrieve the correct member from BASH_REMATCH. Bash does not support non-capturing parentheses, therefore Historian is either under 1 or 2. Fortunately, we know it is the last one.

Sign up to request clarification or add additional context in comments.

Comments

5

Samples pure shell:

a="Historian (Level 1)"
noParens=${a/ \(*/}
lastWord=${noParens/[A-Za-z]* /}

a="Muster Historian (Level 1)"
noParens=${a/ \(*/}
lastWord=${noParens/[A-Za-z]* /}

(It's the same expressions in both cases, just repeated for easy testing).

Comments

0

Based on "And I don't know how to match on the $1 argument."

Have I understood you correctly if what you are asking for is not whether your regex is correct but rather how to perform the match against the contents of your bash variable?

matched_text=$(echo $yourbashvariablecontainingthetext | sed 's/your_regex/backreference_etc/')

$yourbashvariablecontainingthetext should be your $1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.