.* in sed is greedy. And there is no non-greedy AFAIK option.
(You should use [^0-9]* in this case for a non-greedy match, but this only works once, so you only get 678 without 45 )
If you should use only sed , getting the result will not be easy.
I recommend using gnus grep
$ echo hgdfjg678gfdg kjg45nn | grep -oP '\d+' 678 45
If you really want to stick with sed , this will be one of many possible answers.
$ echo hgdfjg678gfdg kjg45nn | \ sed -e 's/\([0-9^]\)\([^0-9]\)/\1\n\2/g' | \ sed -n 's/[^0-9]*\([0-9]\+\).*/\1/p' 678 45
plhn
source share