I have this line of code. The sed is taking 10 and 5. How can I extract 10 and ignore 5?
$ NUMBER=$(echo "The food is ready in 10mins and transport is coming in 5mins." | sed 's/[^0-9]*//g') ; echo $NUMBER
CodePudding user response:
You're just removing everything that isn't a digit, so 5
is left.
Instead, use a capture group to capture the first number, and use that in the replacement.
sed 's/^[^0-9]*\([0-9]*\).*/\1/'
In the regular expression:
^
matches the beginning of the line[^0-9]*
matches all non-digits at the beginning of the line\(
and\)
surround a capture group[0-9]*
matches digits. These are captured in the group.*
matches the rest of the line.
In the replacement:
\1
copies the part of the line that was matched by the capture group. These are the first set of digits on the line.