2017-04-26 191 views
0

我試圖找到包含位於文件名中的文本文件中的字符串的文件,然後將這些文件複製到新目錄中。Cygwin/Linux - 從文件中查找包含字符串的文件

Example 1.txt contains strings line by line: 
1234 
1666 

Directory contains files: 
JOHN-1234-TEXT.CSV 
DAVE-1666-TEXT.CSV 
LAURA-1826-TEXT.CSV 

If code is successful it will copy the files to a new specified directory: 
JOHN-1234-TEXT.CSV 
DAVE-1666-TEXT.CSV 

如果有人能幫我,我將不勝感激。

+0

確保您的圖形文件的Unix的行結束,即'\ N'不是'\ r \ N'。使用'd2u'或'dos2unix'來轉換。 –

回答

0

找到 + grep的 + TR + xargs的管道方法:

find olddir -type f -print | grep -f "1.txt" | tr '\n' '\0' | xargs -0 -I{} cp {} newdir 

olddir目錄包含初始文件

grep -f "1.txt" - 取得從文件模式,每行一個

+0

似乎不起作用,創建一個文件 – kayboxa

+0

@ kayboxa,你應該指定你的實際目錄名而不是'olddir'和'newdir'(它應該工作正常) – RomanPerekhrest

0

快速的解決方案:

while read line;do 
    find -iname "*$line*" -exec cp {} newdir/ \; 
done < string_file 
+0

使用while循環來讀取文件,而不是爲... http ://mywiki.wooledge.org/BashFAQ/001 – Sundeep

相關問題