[colug-432] Nice Plain Text Columns and Avoiding NIH

Jeff Frontz jeff.frontz at gmail.com
Sun Aug 7 22:16:18 EDT 2011


Maybe I'm misunderstanding what it is that you want, but I would think
that such a tool if it existed would (as Jim W suggests) end up being
pretty much the parsing and output formatting components of awk.

awk -F" " ' {printf "%22s %33s %11s", $1, $2, $3 }' # tab character
between double quotes

Unless you were thinking that it would figure out the maximum lengths
of everything and then have an appropriate column size?  Then
something like this:

awk -F" " '{

    for (x = 1; x <= NF; x++)
    {
         save[NR, x] = $x
         len = length($x)
         if (len > maxLen[x])
         {
              maxLen[x] = len
         }
    }

    if (NF > maxParms)
    {
            maxParms = NF
    }

    totalLines++
}

END {
         for (lines = 0; lines <= totalLines; lines++)
         {
                   for (parm = 1; parm <= maxParms; parm++)
                   {
                             format = "%-" maxLen[parm] "s "
                             printf format, save[lines, parm]
                   }

                   printf "\n"
         }
}
'


On Sun, Aug 7, 2011 at 9:42 PM,  <jep200404 at columbus.rr.com> wrote:
> On Sun, 7 Aug 2011 18:05:25 -0400 (EDT), Jim Wildman <jim at rossberry.com> wrote:
>
>> On Sun, 7 Aug 2011, jep200404 at columbus.rr.com wrote:
>>
>> > I have a plain text file with variable width fields delimited by tabs.
>> > What filter program(s) are good at making the columns line up in
>> > plain text output?
>
>> awk is your tool.  See printf output formatting in man awk.
>
> I was hoping to avoid writing my own code for that.
> This seems like the kind of thing that has been solved many
> times before (like for the output of ls -l).
> I was hoping to avoid reinventing (or recoding) the wheel.
>



More information about the colug-432 mailing list