2017-07-12 151 views
0

選擇文件,我有其中有許多成千上萬的文件有相似名稱的文件夾: feed_1.txt, feed_2.txt, feed_3.txt基於名稱

如何選擇僅是feed_40000.txt和更高版本的文件?

+1

您可能會考慮'awk' – dawg

+0

您應該針對特定問題提出具體問題。由於Stack Overflow隱藏了你的密切原因:*「有些問題要求我們推薦或查找書籍,工具,軟件庫,教程或其他非本地資源,因爲它們傾向於吸引自以爲是的答案和垃圾郵件,因此不適合Stack Overflow。 「* – jww

+0

jww im對不起,我不明白你的反對意見,你可以請解釋 – appleLover

回答

0

對於正則表達式的解決方案:

/feed_([4-9][0-9]{4}|[1-9][0-9]{5,})\.txt/g 

這將匹配適合以下兩種格式的字符串:

feed_ab.txt,其中a是從4-9和b。數位是四位數字(爲的情況下40000 < =數字< = 99999或

feed_cd.txt,其中c是來自1-9的數字d d是五位或更多位數(對於100000 < =數字的情況)。

1

您可以使用此awk根據檢查獲得與價值觀>= 40000文件名:要循環這些文件名使用

printf "%s\n" feed_[0-9]* | awk -F '[_.]+' '$2 >= 40000' 

while read -r file; do 
    printf "processing %s\n" "$file" 
done < <(printf "%s\n" feed_[0-9]* | awk -F '[_.]+' '$2 >= 40000') 
1

你可以做

find . -type f -name "feed_*" | awk -F"_" '$2+0>=40000' # => list of file names... 
3

你可以使用查找正則表達式開關:

find . -type f -regextype posix-awk -regex ".*/feed_([4-9]|[123][0-9])[0-9]{4,}\.txt" 
+1

真棒回答先生,從來不知道這個選項也存在於發現,非常不錯感謝分享它。 – RavinderSingh13

+0

'-regextype'是GNU只能作爲註釋找到的。 – dawg

+1

@dawg:默認情況下,regextype是emacs,你可以像這樣寫正則表達式,而不用這個開關,這裏只用來縮短模式:'。*/feed _ \([4-9] \ | [123] [0-9] \)[0-9] [0-9] [0-9] [0-9] + \。txt' –

0

好的,這裏是我的方法(作爲一個學習和實驗與exec和awk)。以下是命令。

find -type f -exec awk --re-interval 'FILENAME ~ /feed_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++{;print FILENAME} END{if(FILENAME ~ /feed_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++){print FILENAME}}' {} \; 

因此以下是相同點。

I-我使用的第一件事--re-interval支持{4,}找到連續出現的0-9數字,但在更新版本的awk中可以刪除。

II-1多學習,我知道了

a- When using \; at last of command it will read the empty size files BUT 

b- When using \+ it will NOT display the 0 size files BECAUSE 

c- We all know \+ collects all the files first then it will perform mentioned action in single shot, so obviously END section will pick only the last file and other files which have ZERO size will NEVER be read. 

編輯:添加命令的非班輪一個形式現在也。

find -type f -exec awk --re-interval \ 
'FILENAME ~ /feed_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++{;print FILENAME} \ 
END{if(FILENAME ~ /feed_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++){print FILENAME}}' {} \;