<http://www.netikka.net/tsneti/info/tscmd096.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Fri 14-May-2010 11:00:41

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



96} Can one use a script to columnize a comma separated values file?

Yes, as shown below
  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Make a test Comma Separated Value file

  set myfile_=MyFile.csv
  > "%myfile_%" echo a,b,c,de
  >>"%myfile_%" echo a2,b2,c2,de2
  >>"%myfile_%" echo 3,b b3,c234567890,de3
  ::
  :: Columnize

  for /f "tokens=1-4 delims=," %%a in ('type "%myfile_%"') do (
    set p1=%%a         .
    set p2=%%b         .
    set p3=%%c         .
    set p4=%%d         .
    echo !p1:~0,9! !p2:~0,9! !p3:~0,9! !p4:~0,9!
    )
  ::
  :: Delete the test file

  for %%f in ("%myfile_%") do if exist %%f del %%f
  endlocal & goto :EOF

The Comma Separated Value file contains
a,b,c,de
a2,b2,c2,de2
3,b b3,c234567890,de3

The test output will be
C:\_D\TEST>cmdfaq
a         b         c         de
a2        b2        c2        de2
3         b b3      c23456789 de3

The method makes it easy to rearrange the columns, if need be. One only has to change the order of the fields on the echo line. This may come handy, in both the directions, in e.g. presenting address books in different formats. The column format is much easier for a human to read than csv-formatted address information.

To accomodate potential exclamation marks the code could be rewritten as (c.f. item #146)
  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: Make a test Comma Separated Value file

  set myfile_=MyFile.csv
  > "%myfile_%" echo a,b,c,de
  >>"%myfile_%" echo a2,b2,c2,de2
  >>"%myfile_%" echo 3,b b3,c234567890,de3
  ::
  :: Columnize

  for /f "tokens=1-4 delims=," %%a in ('type "%myfile_%"') do (
    set p1=%%a         .
    set p2=%%b         .
    set p3=%%c         .
    set p4=%%d         .
    call echo %%p1:~0,9%% %%p2:~0,9%% %%p3:~0,9%% %%p4:~0,9%%
    )
  ::
  :: Delete the test file

  for %%f in ("%myfile_%") do if exist %%f del %%f
  endlocal & goto :EOF

With G(nu)AWK
  @echo off & setlocal enableextensions
  ::
  :: Make a test Comma Separated Value file

  set myfile_=MyFile.csv
  for %%f in ("%myfile_%") do if exist %%f del %%f
  > "%myfile_%" echo a,b,c,de!
  >>"%myfile_%" echo a2,b2,c2,de2
  >>"%myfile_%" echo 3,b b3,c234567890,de3
  ::
  :: Columnize

  <"%myfile_%" unxgawk -F, "{printf\"%%-9s %%-9s %%-9s %%-9s\n\",substr($1,1,9),substr($2,1,9),substr($3,1,9),substr($4,1,9)}"
  ::
  :: Delete the test file

  for %%f in ("%myfile_%") do if exist %%f del %%f
  endlocal & goto :EOF


The output will be
C:\_D\TEST>cmdfaq
a         b         c         de!
a2        b2        c2        de2
3         b b3      c23456789 de3