簡體   English   中英

使用正則表達式過濾搜索文件

[英]Filter files on a search using a regular expression

在 Bash 中,我使用一個數組來存儲我想通過正則表達式復制的文件。 像這樣:

regex_2=(*.awk *.sh *utf8.txt *new.txt)
cp "${regex_2[@]}" $root_dest_dir_2

但是從擴展名為 .sh 的文件中,我想排除那些以這樣的結尾:

*2016025a.sh *2015039.sh *20150810b.sh

因此,排除那些以 .sh 結尾且前面有一個字母和多個數字或以 .sh 結尾且前面有多個數字的那些。

該目錄包含如下文件:

dev_execute_fromFTP5.sh --> to copy
dev_execute_fromFTP5_2016025a.sh -- to exclude
extrac.sh --> to copy
execute_fromFTP5NT.sh --> to copy
execute_fromFTP5NT_20150226.sh --> to exclude
test5.sh --> to copy
.....

您可以將 gnu find 與正則表達式一起使用

find . -regextype egrep \
       -regex ".*\.(awk|sh|utf8\.txt|new.txt)" \
       ! -regex ".*([0-9][[:alnum:]]).sh" \
       -exec cp {} $root_dest_dir_2 +

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM