Created
February 8, 2013 02:30
-
-
Save kawaz/4736162 to your computer and use it in GitHub Desktop.
列幅がバラバラなテキストを良い感じに見やすく整形してくれるawkスクリプト。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/awk -f | |
BEGIN { | |
_sep = sep ? sep : " " | |
} | |
{ | |
split($0, cols) | |
for(i=1; i<=length(cols); i++) { | |
col_size[i] = max(col_size[i], length(cols[i])) | |
} | |
rows[NR]=$0 | |
} | |
END { | |
for(row_i=1; row_i<=length(rows); row_i++) { | |
split(rows[row_i], cols) | |
print_fmt(cols, _sep) | |
} | |
} | |
function print_fmt(cols, sep) { | |
for(i=1; i<=length(cols); i++) { | |
col = cols[i] | |
if(1 < i) { | |
printf sep | |
} | |
printf "%" (col ~ /^[0-9]/ ? "" : "-") col_size[i] "s", col | |
} | |
printf "\n" | |
} | |
function max(a, b) { | |
return a < b ? b : a | |
} |
数字っぽい値は右寄せにしてくれる気の利かせようw
awkなんでCSVをフォーマットしたいときは-F,
とかのオプションを付けてやればよい
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
例えば↓これが…
↓こうなる!