2017-02-04 151 views
2

我試圖使用bash來重命名與特定模式匹配的目錄中的所有.txt文件。我下面的兩個嘗試從目錄中刪除了這些文件並拋出一個錯誤。謝謝:)重命名目錄中的特定類型的所有文件

輸入

16-0000_File-A_variant_strandbias_readcount.vcf.hg19_multianno_dbremoved_removed_final_index_inheritence_import.txt 
16-0002_File-B_variant_strandbias_readcount.vcf.hg19_multianno_dbremoved_removed_final_index_inheritence_import.txt 

所需的輸出

16-0000_File-A_multianno.txt 
16-0002_File-B_multianno.txt 

猛砸試圖1 this removes the files from the directory

for f in /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt ; do 
# Grab file prefix. 
p=${f%%_*_} 
bname=`basename $f` 
pref=${bname%%.txt} 
mv "$f" ${p}_multianno.txt 
done 

猛砸試圖2 Substitution replacement not terminated at (eval 1) line 1.

for f in /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt ; do 
# Grab file prefix. 
p=${f%%_*_} 
bname=`basename $f` 
pref=${bname%%.txt} 
rename -n 's/^$f/' *${p}_multianno.txt 
done 

回答

3

你不需要循環。 rename單獨能夠做到這一點:

rename -n 's/(.*?_[^_]+).*/${1}_multianno.txt/g' /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt 

正則表達式的意思大致是, 捕捉一切從一開始,直到第二_, 比賽休息, 與捕獲的前綴替換和追加_multianno.txt

使用-n標誌,該命令將打印它將執行的操作,而不實際執行該操作。 當輸出看起來不錯時,請刪除-n並重新運行。

+1

非常感謝您的幫助和解釋,我非常感謝他們:) – Chris

相關問題