]> git.nbdom.net Git - nb.git/commitdiff
csv2human
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 28 Feb 2016 20:57:23 +0000 (20:57 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 28 Feb 2016 20:57:23 +0000 (20:57 +0000)
etc/profile.d/functions

index 76467c308fb2117d8379cb1002443f9e5adf2090..1f513ae255504347a122105126d8f3fc20221c27 100644 (file)
@@ -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
+}