0

リストの並べ替えに問題があります。弦を組み立てる方法だと思います。しかし、詳細を見てみましょう:

大きな (windows)-txt ファイルがあります。これらはホットフィックスの Readme です。次のように、このリリースで解決された問題のある HotFix-Number を抽出したいと思います。

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

ファイルを次々と計算するループがあります。このループでは、いくつかの抽出操作の後、HotFix-Number 用の 1 つの文字列変数と、HotFix-Number に属するテキストを含む tmp4.txt があります。

$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
4

1 に答える 1

3

${array[@]}次のように、 の周りに引用符を追加する必要があります。

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

これにより、bash が配列エントリ内のスペースを再解釈するのを防ぐことができます。

于 2013-05-29T09:58:43.027 に答える