I have an html-type of file that somewhere includes a tag as follows:


How do I catch in a variable the value 0.3.5 and only this?

I have come as far as grepping the line as follows:

grep -E "<Currentnumber>.*</Currentnumber>$" myfile

I would appreciate if for educational purposes any answer also provided an explanation of the grep parameters used.

Pls note that the line starts with some empty spaces (or tabs), this is why the following will not work

grep -E "^<Currentnumber>.*</Currentnumber>$" myfile
  • 3
    Obligatory link to Using regular expressions with HTML tags
    – Jeff Schaller
    Commented Feb 13, 2018 at 17:39
  • 2
    HTML and XML are not regular languages and one should not be attempting to parse them with regular expressions.
    – DopeGhoti
    Commented Feb 13, 2018 at 17:40
  • @DopeGhoti so what is the best/recommended way of going about this?
    – pkaramol
    Commented Feb 13, 2018 at 17:41
  • 2
    HTML or XML parsers exist. Use the right tool for the right job. Such as xmllint --xpath.
    – DopeGhoti
    Commented Feb 13, 2018 at 17:43

3 Answers 3


Never parse html with regex ! That's the most common mistake on stackexchange sites.

Better use a XML/HTML parser like , or saxon-lint my own project

Examples :

xmllint --xpath '//Currentnumber/text()' file.html
xmlstarlet sel -t -v '//Currentnumber/text()' file.html
saxon-lint --xpath --html '//Currentnumber/text()' file.html

Check: Using regular expressions with HTML tags

  • 1
    I am on an immutable container with no option of installing something and these tools are not available.
    – pkaramol
    Commented Feb 14, 2018 at 8:14
  • Tell your sysOp or devOps to add them in the container Commented Feb 14, 2018 at 13:48
  • @GillesQuenot - "Go tell your devops to add xmllint to the container" is delusional of how production works, and uncalled-for rudeness. Commented Oct 15, 2021 at 17:27

Use sed and regex

sed -e 's/<Currentnumber>\(.*\)<\/Currentnumber>/\1/' file.html
  • Upvoted, because someone downvoted it for wrong reasons. I like my approach better, but this is also clean. Commented Oct 15, 2021 at 17:47
  • No, this is not clean Commented Feb 20 at 18:15

Hopefully this helps the next person:

$ echo '<Currentnumber>0.3.5</Currentnumber>' | cut -d '>' -f 2 | cut -d '<' -f1

About this answer:

  • Your string/haystack is "known", yeah? So tricks like this are fair.
  • I'll point out the flaw: If someone changes the markup, this solution may need to be updated. Fine. (That is still no reason to resort to non-performant and less maintainable regex.. not for "this")
  • (Maybe make a test or sanity check that you always get a build number back, so such a change is noticed quickly)
  • No special tool needed... even if you have a minimal shell, cut will be there. (The answer to "ask devops to install a linter inside the container" is going to be a complete non-starter)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .