我有名为:
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
有什么简单的方法可以做到这一点吗?
如果您只需要号码:
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 更新。
试试这个:
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
find . -name "example*" -type d | awk -F"example" '{print $NF}'