2013-05-29 131 views
0

我在排序列表時遇到問題。我認爲它是我把琴絃放在一起的方式。但讓我們看看細節:在bash中排序列表

我有大的(Windows)的TXT文件。這些是自述文件的修補程序。我想這個問題,這是解決了在此版本中,這樣提取的HotFix號碼:

1378 Issue: Here is the issue that is fixed 
1390 Issue: Another issue is fixed 
1402 Issue: Yet another fixed issue 

我有一個循環接一個計算一個文件。在這個循環中,在一些抽取操作之後,我有一個字符串變量用於HotFix-Number,tmp4.txt用於屬於HotFix-Number的文本。

$NR=1378 
cat tmp4.txt - Output: Issue: Here is the issue that is fixed 

在循環結束時,我把這些2個組件一起:

array[IDX]=$(echo $NR $(cat tmp4.txt)); 

循環結束後,我檢查了各指標的內容。如果我贊同的單品,我得到正確的形式:

echo ${array[0]} #output: 1390 Issue: Another issue is fixed 
echo ${array[1]} #output: 1378 Issue: Here is the issue that is fixed 
echo ${array[2]} #output: 1402 Issue: Yet another fixed issue 
...  

但是,當我想用​​排序

for j in ${array[@]}; do echo "$j"; done | sort -n >> result.txt; 

列表中我得到所有單個的詞進行排序按字母順序排列的文件。但我只想提到HotFix-Number。

# Sampleoutput from result.txt for these 3 examples 
Another 
another 
fixed 
fixed 
fixed 
Here 
... 
Yet 
1378 
1390 
1402 
+0

請更新問題..沒有意義。特別是最後一句話! –

+0

[對多個部分項目排序列表]的可能重複(http://stackoverflow.com/questions/16812914/sorting-a-list-with-multi-part-items) –

回答

3

您需要添加引號${array[@]},像這樣:

for j in "${array[@]}"; do echo "$j"; done | sort -n >> result.txt; 

這將阻止慶典重新詮釋你的數組項內的空間。

+0

哇謝謝!這將做到這一點。但是現在我有一些問題:還有像3312.1這樣的數字。像這樣的所有數字都在排序輸出的末尾。 –

+0

小數似乎很適合我排序。我建議你發佈一個新問題,並顯示(排序)數據以及出來的數據(樣本)。 – ams