Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract numbers from filename

Tags:

bash

sed

In BASH I thought to use sed, but can't figure how to extract pattern instead usual replace.

For example:

FILENAME = 'blah_blah_#######_blah.ext'

number of ciphers (in above example written with "#" substitute) could be either 7 or 10

I want to extract only the number

like image 725
zetah Avatar asked Sep 04 '11 17:09

zetah


1 Answers

If all you need is to remove anything but digits, you could use

ls | sed -e s/[^0-9]//g

to get all digits grouped per filename (123test456.ext will become 123456), or

ls | egrep -o [0-9]+

for all groups of numbers (123test456.ext will turn up 123 and 456)

like image 94
mvds Avatar answered Sep 30 '22 14:09

mvds