linux_expr_command

This is an old revision of the document!


I think the Linux expr command is woefully underused for matching and extraction of substrings using basic regular expressions, so herewith a quick primer.

Consider a fully-qualified package name with version number and, say, patch level number:

$ P="fubar-1.2.3-42"

Say one wanted to match or extract different components of that string – this variation of the expr command will, first, simply count the number of matching characters based on an anchored match; that is, anchored to the beginning of the string.

Let's count the length of the substring that matches anything but that first hyphen:

$ expr $P : '[^-]*'
5
$

Moderately useful, I guess, so let's extend this by counting the number of characters in the combined package number and version:

$ expr $P : '[^-]*-[^-]*'
11
$
  • linux_expr_command.1568801099.txt.gz
  • Last modified: 2019/09/18 10:04
  • by rpjday