Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_expr_command [2019/09/18 10:02] – rpjday | linux_expr_command [2019/09/18 10:44] (current) – rpjday | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Overview ===== | ===== Overview ===== | ||
- | I think the Linux '' | + | I think the Linux '' |
Consider a fully-qualified package name with version number and, say, patch level number: | Consider a fully-qualified package name with version number and, say, patch level number: | ||
Line 10: | Line 10: | ||
Say one wanted to match or extract different components of that string -- this variation of the '' | Say one wanted to match or extract different components of that string -- this variation of the '' | ||
+ | |||
+ | 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): | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | 5 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | So that's the length of the package name " | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | 11 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | That is clearly the number of characters in the substring " | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | 14 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | "So what?", | ||
+ | |||
+ | Knowing we need only supply enough pattern to match from the beginning, extract the package name: | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | fubar | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | Extract the version number by tagging that second field (making sure to start with enough regular expression to skip over the package name first): | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | 1.2.3 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | 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): | ||
+ | |||
+ | < | ||
+ | $ expr $P : ' | ||
+ | 42 | ||
+ | $ | ||
+ | </ | ||
+ | |||
+ | 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} |