#!/bin/bash
result=$(find . -type f -name -regex '\w{8}[-]\w{4}[-]\w{4}[-]\w{4}[-]\w{12}')
echo $result
我嘗試了上述但使用了一個變數,我有點迷茫。
uj5u.com熱心網友回復:
mapfile -td '' myarray \
< <(find . -type f -regextype egrep \
-regex '.*/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}' -print0)
# iterate the array in a loop
for i in "${myarray[@]}"; do
echo "$i"
done
假設您正在尋找與此正則運算式匹配的檔案。
uj5u.com熱心網友回復:
我打賭find默認情況下使用基本的正則運算式,所以\w可能不知道,并且大括號必須被轉義。添加一個-regextype選項(并洗掉 -name):
re='.*[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}.*'
find . -type f -regextype egrep -regex "$re"
請注意,我的find手冊頁說的是-regex:
這是對整個路徑的匹配,而不是搜索。
這意味著前導和尾隨.*是確保模式匹配整個路徑所必需的。
并將結果捕獲到陣列中(假設檔案名中沒有換行符),請使用 bashmapfile命令,從行程替換重定向。
mapfile -t files < <(find . -type f -regextype egrep -regex "$re")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354261.html
上一篇:void函式不回傳任何內容-C
下一篇:如何從該陣列中回顯成員資格值?
