配列を通るループ

配列を通るループ

list.txt(INPUT)の私の記録は次のとおりです。

List of animals SET 1=Dog 2=Crow 3=Snake Pet,bird,reptile
List of Countries SET 1=France 2=Singapore 3=Columbia Europe,Asia,SouthAmerica
List of Parts SET 1=KeyBoard 2=Speaker 3=LEDpanel Computer,AudioPlayer,Television
List of Cities SET 1=Amsterdam 2=KualLumpur 3=Paris Netherlands,Malaysia,France

たとえば、各行の最後の列を配列として使用して数字1,2,3を置き換えようとします。Pet,bird,reptile最初の行を使用してPet=Dog bird=Crow合計を計算しますreptile=Snake

したがって、出力ファイルは次のようになります。

List of animals SET Pet=Dog bird=Crow reptile=Snake
List of Countries SET Europe=France Asia=Singapore SouthAmerica=Columbia
List of Parts SET Computer=KeyBoard AudioPlayer=Speaker Television=LEDpanel
List of Cities SET Netherlands=Amsterdam Malaysia=KualLumpur France=Paris

を使用すると、awk最後の列を配列文字列に分割できます。しかし、1、2、3を同じ数字に置き換えることはできません。

ベストアンサー1

a構文を使用してawk配列の項目を繰り返すことができますfor (i in a)。たとえば、次の操作を実行できます。

awk '{split($NF,a,","); $NF=""; for (i in a) sub(i"=",a[i]"=",$0); print}' list.txt

おすすめ記事