从文件名中提取号码 [英] extract numbers from file names

查看:103
本文介绍了从文件名中提取号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了很多文件,他们的名字都是这样的:

  tmp1.csv
tmp32.csv
tmp9.csv
tmp76.csv
...

它们是在相同的目录,并欲在文件名中提取号码。我怎样才能做到这一点在庆典

PS

我试过的grep ,但无法做到这一点。此外,我试过 $ {文件名## [A-Z]} 。难道他们走正道?


解决方案

 在* .csv的F;做回声$ F | egrep的-o[0-9] +; DONE

如果您有其他csv文件在其文件名的数字是:

 在* .csv的F;做回声$ F | SED -re的/ tmp目录^([0-9] +)\\ $ CSV / \\ 1 /'; DONE

I got a lot of files whose names are like this:

tmp1.csv
tmp32.csv
tmp9.csv
tmp76.csv
...

They are in the same dir, and I want to extract the numbers in the file name. How can I do that in bash?

PS

I tried grep, but can't make it. Also I tried ${filename##[a-z]}. Are they the right way to go?

解决方案

for f in *.csv; do echo $f | egrep -o "[0-9]+" ; done

If you have other csv file with digits in their filenames use:

for f in *.csv; do echo $f | sed -re 's/^tmp([0-9]+)\.csv$/\1/' ; done

这篇关于从文件名中提取号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆