2

我有名为:

example1 example2 example3

我想从他们每个人中提取示例编号。就像是:

for exampleSubfolder in `find . -type d`
do
  example_number= #replace 'example' in $exampleSubfolder with empty string
  #do other stuff in this subfolder
done

有什么简单的方法可以做到这一点吗?

4

3 回答 3

4

如果您只需要号码:

find . -type d -name 'example*' | egrep -o "[0-9]+"

但是如果您想知道文件夹名称和编号之间的对应关系:

for f in $(find . -mindepth 1 -maxdepth 1 -type d -name 'example*')
do
   number=${f#example}
done

用字符串替换 bashism 更新。

于 2012-08-13T09:49:31.453 回答
2

试试这个:

for DIR in /path/to/search/example*; do
  if [ ! -d $DIR ]; then continue; fi
  NUMBER=$(echo $DIR | grep -Eo '[0-9]+$')
  pushd $DIR
    # Do stuff here
  popd
done
于 2012-08-13T09:51:30.883 回答
2
find . -name "example*" -type d | awk -F"example" '{print $NF}'
于 2012-08-13T09:53:21.410 回答