2016-02-29 89 views
0

我有一個包含文件的目錄編號這樣文件進行排序,然後對他們中的每一個

1>chr1:2111-1111_mask.txt 
1>chr1:2111-1111_mask2.txt 
1>chr1:2111-1111_mask3.txt 
2>chr2:345-678_mask.txt 
2>chr2:345-678_mask2.txt 
2>chr2:345-678_mask3.txt 
100>chr19:444-555_mask.txt 
100>chr19:444-555_mask2.txt 
100>chr19:444-555_mask3.txt 

每個文件都包含像在第一線>chr1:2111-1111的名稱,並在第二一系列字符的執行命令線。 我需要使用>之前的數字作爲參考,在數字上對此目錄中的文件進行排序,對_mask3中的每個文件執行命令並使用。

我有這樣的代碼

ls ./"$INPUT"_temp/*_mask3.txt | sort -n | for f in ./"$INPUT"_temp/*_mask3.txt 
    do 
    read FILE 
    Do something with each file and list the results in output file including the name of the string 
    done 

它的工作原理,但是當我檢查輸出文件中的字符串列表,他們都是這樣

>chr19:444-555 
>chr1:2111-1111 
>chr2:345-678 

爲什麼呢?

+1

您最好不要將'>'字符作爲文件名的一部分。在處理名稱錯誤的地方,它被解釋爲一個命令而不是文件名,'> chr19:444-555'會將'chr19:444-555'截斷爲'0'或將stdin中的任何內容重定向到文件' chr19:444-555' –

+0

[你不應該爲你的腳本解析'ls']的輸出(http://mywiki.wooledge.org/ParsingLs),對於初學者!不知道在這種情況下替代是什麼。 – miken32

回答

0

所以......我不確定這裏有什麼「有效」,就像你說的問題一樣。

看起來好像你有兩個問題。

  1. 你的文件不在有序
  2. 文件名稱具有領先的數字去掉

尋址1,你的命令ls ./"$INPUT"_temp/*_mask3.txt | sort -n | for f in ./"$INPUT"_temp/*_mask3.txt這裏不作一大堆的道理。您正在從ls獲取文件列表,然後管理這些文件進行排序。這可能會給你你正在尋找的輸出,但是你把它輸給for,這沒有任何意義。

其實你可以重寫你的整個腳本

for f in ./"$INPUT"_temp/*_mask3.txt 
    do 
    read FILE 
    Do something with each file and list the results in output file including the name of the string 
    done 

你會具有完全相同的輸出。爲了得到這個排序,你可以這樣做:

for f in `ls ./"$INPUT"_temp/*_mask3.txt | sort -n` 
    do 
    read FILE 
    Do something with each file and list the results in output file including the name of the string 
    done 

至於意外截斷,在您的文件名>字符是在你的bash shell重要,因爲它指示前面的命令的標準輸出到指定的文件。您需要確保當您使用循環中的變量$f時,您會堅持引用該事件以防止bash誤解文件名稱command > file類型的事物。

相關問題