]> git.nbdom.net Git - nb.git/commitdiff
option --format
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 20 Apr 2023 00:56:15 +0000 (02:56 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 20 Apr 2023 00:56:15 +0000 (02:56 +0200)
bin/csv2human

index 93312e9ebd92b8cea2ab8c56f3ea774de30027de..5eb27e879d66466ab442b1d68555cf95e1cf7e34 100755 (executable)
@@ -47,11 +47,10 @@ my @lines = ();
 
 while (<>) {
 
-       my @F = split(/$Opt{sep}/);
+       my @F = $Opt{format} ? split(/\s*$Opt{sep}\s*/) : split(/$Opt{sep}/);
 
        for (my $i=0;$i<@F;$i++) {
                chomp($F[$i]);
-               #$F[$i] =~ s/^\r//;
                $F[$i] = Encode::decode_utf8($F[$i]);
                my $l;
                $len[$i] = $l if ( $l = length($F[$i]) ) >= ($len[$i] || 0);
@@ -62,15 +61,20 @@ while (<>) {
 
 exit unless @lines;
 
-# NB 11.07.19 Bug Wrong ! my $tot = @lines - ($Opt{header} ? 0 : 1);
 my $tot = @lines - ($Opt{header} ? 1 : 0);
 
 my $t = -1;
-my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10);
 
+my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10);
 my $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10);
-print $sep_line;
 
+if ($Opt{format}) {
+       my $sep = " ".$Opt{sep}." ";
+       $format = join($sep,map {$t+=$_+length($sep); "\%-".$_."s"} @len).chr(10);
+       $sep_line = "";
+}
+
+print $sep_line;
 my $i = 0;
 my $count;
 while (my $line = shift @lines)
@@ -210,6 +214,7 @@ Description!
 
  -option[header|h!]    Print or not header separotor after first row
  -option[sep|s=s]       Separtor
+ -option[format|f!]       Only format according to option --sep. Usefull for coding
  -option[verbose|v+]   Verbose mode: increase the verbosity level.
  -option[debug+]       Debug mode: increase the verbosity level.
  -option[version|V]   Print version (default: $VERSION)