2013-12-10 45 views
0

我有大約1000文件的文件夾是這樣的:如何通過文件夾更改文件名?

$ awk '{print $9}' folder 
1003ns_cells_found.fcs 
1003pi_cells_found.fcs 
1007ns_cells_found.fcs 
1007pi_cells_found.fcs 
1029ns_cells_found.fcs 
1029pi_cells_found.fcs 
1041nsA_cells_found.fcs 

而且我還有一個ID文件是這樣的:

$ head -5 ID 
1003 BD2188 
1003 BD2188 
1007 BD2116 
1007 BD2116 
1029 BD2012 

我會與替換文件夾中的所有文件的ID號ID文件的第二列。有誰知道如何通過Terminal/Shell實現它?

+0

您的示例數據似乎是兩個文件之間的「陣容」。那是有意的還是你想在你的ID文件中只有一行像'1003 BC2188'?換一種說法,您的ID文件中是否也有大約1000行?此外,請編輯您的問題,包括給出這些輸入的預期樣本輸出。當你在主題行中顯示「更改文件...」時,你想發出'mv'還是'rename' cmds。像'mv 1003ns_cells_found.fcs BD2188ns_cells_found.fcs'?祝你好運。 – shellter

回答

2

這個簡單的腳本應該這樣做:

#!/bin/bash 

ls ./folder | while read line; do 
cat ID | awk -v x=${line:0:4} 'x==$1 {printf "%s", $2; exit}' 
echo -e "${line:4}"; 
done 

的文件夾結構如下所示:

$ find 
. 
./ID <-- file with the IDs 
./folder 
./folder/1003pi_cells_found.fcs 
./folder/1041nsA_cells_found.fcs 
./folder/1007pi_cells_found.fcs 
./folder/1007ns_cells_found.fcs 
./folder/1029pi_cells_found.fcs 
./folder/1003ns_cells_found.fcs 
./folder/1029ns_cells_found.fcs 
./script <-- script to execute 

輸出:

BD2188ns_cells_found.fcs 
BD2188pi_cells_found.fcs 
BD2116ns_cells_found.fcs 
BD2116pi_cells_found.fcs 
BD2012ns_cells_found.fcs 
BD2012pi_cells_found.fcs 
nsA_cells_found.fcs 

最後一個是空的,因爲1041沒有找到在文件ID

相關問題