Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed extracting group of digits

Tags:

I have tried to extract a number as given below but nothing is printed on screen:

echo "This is an example: 65 apples" | sed -n  's/.*\([0-9]*\) apples/\1/p' 

However, I get '65', if both digits are matched separately as given below:

echo "This is an example: 65 apples" | sed -n  's/.*\([0-9][0-9]\) apples/\1/p' 65 

How can I match a number such that I don't know the number of digits in a number to be extracted e.g. it can be 2344 in place of 65?

like image 625
Usman Avatar asked Feb 13 '12 12:02

Usman


1 Answers

$ echo "This is an example: 65 apples" | sed -r  's/^[^0-9]*([0-9]+).*/\1/' 65 
like image 106
codaddict Avatar answered Mar 16 '23 05:03

codaddict