Skip to content

Instantly share code, notes, and snippets.

@gin135
Last active July 4, 2017 03:48
Show Gist options
  • Save gin135/7a9c01eabba7f23f62dc38f1d3f9e8bc to your computer and use it in GitHub Desktop.
Save gin135/7a9c01eabba7f23f62dc38f1d3f9e8bc to your computer and use it in GitHub Desktop.
第29回シェル芸勉強会 Q4(4.1, 4.2両方)
#!/bin/sh
#Q4.1
echo -1 4 5 2 42 421 44 311 -9 -11 | # 数列の出力
tr ' ' '\n' | # 1レコード1フィールドに整形
sort -n | # 数値順でソート
awk '{print $0,sub(/-/, "", $0),gsub(/[0-9]/, "", $0)}' | # 1:数値, 2:正負フラグ 3:数値長 を出力
awk '{arr[$2,$3] = arr[$2,$3]" "$1} END{for(n in arr){print arr[n]}}' | # 2:正負フラグ,3:数値長 をキーとして、1:数値を集計
sort -k 1,1n # カテゴリ順にソート
#Q4.2
echo -1 +4 5 2 42 421 44 311 -9 -11 |
tr ' ' '\n' |
sort -n |
awk '{print $0,sub(/-/, "", $0),gsub(/[0-9]/, "", $0)}' |
awk '{arr[$2,$3] = arr[$2,$3]" "$1} END{for(n in arr){print arr[n]}}' |
sort -k 1,1n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment