From: Nicolas Boisselier Date: Sun, 28 Feb 2016 20:57:23 +0000 (+0000) Subject: csv2human X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=2de7cd888f630b88660c8dde5ad40d3f144c6b0b;p=nb.git csv2human --- diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 76467c30..1f513ae2 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -431,3 +431,52 @@ exit $diff ? 0 : 1; [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" } + +csv2human() { + shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; + declare sep noheader; + sep='\t'; + noheader=0; + while [ $# -gt 0 ]; do + case "$1" in + --sep|-s) + sep=$2; + shift + ;; + --noheader|-nh) + noheader=1 + ;; + *) + echo "Unknow option: $1 at $0!"; + exit -1 + ;; + esac; + shift; + done; + [ -z "$@" ] || sep="$@"; + cat | perl -MEncode -F"$sep" -ane 'BEGIN { + $noheader = shift @ARGV; + @len = (); @lines = (); + binmode( STDOUT, "utf8:" ); +}; +for ($i=0;$i<@F;$i++) { + chomp($F[$i]); + $F[$i] = Encode::decode_utf8($F[$i]); + $len[$i] = $l if ( $l = length($F[$i])) >= $len[$i]; +} +push @lines, [@F]; +END { + exit unless @lines; + $i = 0; + $t = -1; + $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10); + $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); + print $sep_line; + while ($_ = shift @lines) { + printf $format,@$_; + print $sep_line if !$noheader and !$i++; + } + print $sep_line; +} +' $noheader +}