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:32] – rpjday | linux_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 '' | 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): | + | 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): |
< | < | ||
Line 27: | Line 27: | ||
</ | </ | ||
- | 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 " | + | That is clearly the number of characters in the substring " |
< | < | ||
Line 62: | Line 62: | ||
Piece of cake. Note that this works for only one tagged field -- if you tag more than one, you get just the first. | 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} |