Linux シェル

sort

#!/bin/sh -eu

# 空白区切りで要素を収集
fruits="banana"
fruits=${fruits}" banana"
fruits=${fruits}" apple"
fruits=${fruits}" lemon"
fruits=${fruits}" apple"
echo ${fruits}

# 配列へ
fruits=(${fruits})
echo ${fruits[@]}

# 重複排除しソート
fruits=($(for v in "${fruits[@]}"; do echo "$v"; done | sort -u ))
echo ${fruits[@]}

# ループ処理
for fruit in "${fruits[@]}"; do
    echo $fruit
done
#!/bin/bash

# 以下のメンバーを年齢降順にソート
members="Taro=engineer@35 Jiro=dentist@30 Saburo=lawyer@25 Shiro=chef@40"
members=(${members})
members=($(for v in "${members[@]}"; do echo "$v"; done | sort -u -n -r -k 2 -t @ ))
echo ${#members[@]}

n=3
for member in "${members[@]:0:$n}"; do  # 最初の3人だけ表示
    name=${member%=*}
    occupation=${member#*=}
    occupation=${occupation%@*}
    age=${member#*@}
    echo $name $occupation $age
done