0
hashrate=${line//*:/}
hashrate=${hashrate//H\/s/}

I'm trying to unify this regex replace into a single command, something like:

hashrate=${line//*:\+/H\/s/}

However, this last option doesn't work. I also tried with \|, but it doesn't seem to work and I haven't found anything useful in bash manuals and documentation. I need to use ${} instead of sed, even if using it solves my problem.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
aegroto
  • 43
  • 3

2 Answers2

2

The alternation for shell patterns (assuming extended globbing, shopt -s extglob is enabled), is @(pattern|pattern...). For your case:

${line//@(*:|H\/s)}

The trailing / is optional if you just remove a pattern instead of replacing it.

Notice that because of the double slash, //, all occurrences of the patterns will be removed, one at a time. If you used *(...) (see randomir's answer), consecutive patterns would be removed all in one go. Unless you have giant string, the difference should be negligible. (If you have giant strings, you don't want to use globbing anyway, as it's not optimized for this kind of thing.)

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
1

If you enable extended globbing (extglob via shopt), you can use the *(pattern1|pattern2|...) operator to match zero or more glob patterns:

hashrate="${line//*(*:|H\/s)/}"
randomir
  • 17,989
  • 1
  • 40
  • 55