0

我在排序列表时遇到问题。我认为这是我将琴弦放在一起的方式。但是让我们看看细节:

我有大型(Windows)-txt 文件。这些是修补程序的自述文件。我想提取带有问题的 HotFix-Number,在此版本中已解决,如下所示:

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

我有一个循环计算一个又一个文件。在这个循环中,经过一些提取操作后,我有 1 个用于 HotFix-Number 和 tmp4.txt 的字符串变量,其中的文本属于 HotFix-Number。

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

在循环结束时,我将这两个组件放在一起:

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
4

1 回答 1

3

您需要在 周围添加引号${array[@]},如下所示:

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

这将防止 bash 重新解释数组条目中的空格。

于 2013-05-29T09:58:43.027 回答