[英]Sorting a list in 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创建了1个字符串变量,其文本属于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编号。
# Sampleoutput from result.txt for these 3 examples
Another
another
fixed
fixed
fixed
Here
...
Yet
1378
1390
1402
您需要在${array[@]}
周围添加引号,如下所示:
for j in "${array[@]}"; do echo "$j"; done | sort -n >> result.txt;
这样可以防止bash重新解释数组条目内的空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.