Created
December 7, 2017 07:07
-
-
Save lufia/1856ca17f26a276deb337c437c389751 to your computer and use it in GitHub Desktop.
convert empty line separated key value pair files to table
This file contains 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
#!/usr/bin/env bash | |
flagh=0 | |
case $1 in | |
-h) flagh=1 | |
shift | |
;; | |
-*) echo usage: $(basename $0) [-h] [file ...] >&2 | |
exit 1 | |
;; | |
esac | |
awk -v "flagh=$flagh" ' | |
BEGIN { | |
n = 1 | |
wid = 10 | |
} | |
NF == 0 { | |
n++ | |
next | |
} | |
NF != 2 { | |
printf "warning: too many fields near %s\n", $0 >>"/dev/stderr" | |
next | |
} | |
{ if(!($1 in keys)){ | |
keys[$1] = 1 | |
keyseq[++nkeys] = $1 | |
tab[0,$1] = $1 | |
if(flagh && length($1) > wid) | |
wid = length($1) | |
} | |
if(length($2) > wid) | |
wid = length($2) | |
tab[n,$1] = $2 | |
} | |
END { | |
bp = 1 | |
if(flagh) | |
bp = 0 | |
for(i = bp; i <= n; i++){ | |
for(j = 1; j <= nkeys; j++){ | |
if(j > 1) | |
printf " " | |
if((i,keyseq[j]) in tab) | |
printf "%-*s", wid, tab[i,keyseq[j]] | |
else | |
printf "%-*s", wid, "-" | |
} | |
printf "\n" | |
} | |
} | |
' "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment