linux_expr_command

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux_expr_command [2019/09/18 10:10] rpjdaylinux_expr_command [2019/09/18 10:44] (current) rpjday
Line 11: Line 11:
 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. 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 initial substring that matches anything but a hyphen:+Let's count the length of the initial substring that matches anything but a hyphen (remember, this is a pattern match that is anchored to the beginning of the string, so it will match everything up to, but not including, the first hyphen):
  
 <code> <code>
Line 19: Line 19:
 </code> </code>
  
-Questionably useful, I guess, so let's extend this by counting the number of characters in the //combined// package number and version (and intervening hyphen):+So that's the length of the package name "fubar"Questionably useful, I guess, so let's extend this by counting the number of characters in the //combined// package number and version (and intervening hyphen):
  
 <code> <code>
Line 27: Line 27:
 </code> </code>
  
-Finally, we could, of course, use that trick to match the entire string based on the pattern we know it will have (allowing a simple ".*" to suck up the rest of the string):+That is clearly the number of characters in the substring "fubar-1.2.3"Finally, we could, of course, use that trick to match the entire string based on the pattern we know it will haveallowing a simple ".*" to suck up the rest of the string (and, yes, this is overkill, but we're going somewhere with this):
  
 <code> <code>
Line 34: Line 34:
 $ $
 </code> </code>
 +
 +"So what?", you think. Ah, but you can also //tag// the field you care about to have that field value printed, not simply its length.
 +
 +Knowing we need only supply enough pattern to match from the beginning, extract the package name:
 +
 +<code>
 +$ expr $P : '\([^-]*\)'
 +fubar
 +$
 +</code>
 +
 +Extract the version number by tagging that second field (making sure to start with enough regular expression to skip over the package name first):
 +
 +<code>
 +$ expr $P : '[^-]*-\([^-]*\)'
 +1.2.3
 +$
 +</code>
 +
 +Finally, extract the patch level by tagging the trailing substring after the second hyphen (again, supplying enough initial RE to skip over the package name and version number to get there):
 +
 +<code>
 +$ expr $P : '[^-]*-[^-]*-\(.*\)'
 +42
 +$
 +</code>
 +
 +Piece of cake. Note that this works for only one tagged field -- if you tag more than one, you get just the first.
 +
 +P.S. If the string you're manipulating has only two fields of interest, or has multiple fields of which only the first and last will ever be of interest, then you can of course use standard variable substitution:
 +
 +  * ${var#ptn}
 +  * ${var##ptn}
 +  * ${var%ptn}
 +  * ${var%%ptn}
  • linux_expr_command.1568801446.txt.gz
  • Last modified: 2019/09/18 10:10
  • by rpjday