0

I have a string like this: line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."

I want to extract 46942 and put in a variable.

I tried this, but it only removes the Processed and total. How do I do it?

Var=$(echo $line | sed -e 's/Processed\(.*\)total/\1/')
Yamuna_dhungana
  • 653
  • 4
  • 10
  • assuming the expected result is `var=46942`, choroba's parameter expansion answer will likely be the fastest (and most efficient) approach; if OP wishes, for whatever reason, to keep the `sed` approach (and the extra overhead for invoking a sub-shell), the following should work: `var=$(sed -E 's/^.*Processed ([0-9]*) .*$/\1/g' <<< "${line}") ; echo "${var}"` => `46942` – markp-fuso Jan 12 '21 at 15:36

1 Answers1

1

Use parameter expansion:

line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
line=${line#*'Processed '}  # Remove from left up to "Processed ".
line=${line%' total'*}      # Remove from from " total" up to the end.
choroba
  • 231,213
  • 25
  • 204
  • 289