Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

formatting columns in an awk output

215 views
Skip to first unread message

z.entropic

unread,
Jul 30, 2008, 9:46:28 AM7/30/08
to
I can't figure out how to force the awk output to begin in a certain
column after the file name is printed by a separate column, to have a
nicely aligned output. I've tried piping through fmt and sed 's/ /\t/
g', but nothing does what I want...

The one-liner script that prints selected fields on the last line of
each record in temp is:

for i in temp/*;do print -n $i" ";tail -1 $i|awk '{printf "%2.2f %4.1f
%1.3f\n", $4/3600/24, $8*1e6, $9}';done

The following example illustrates the formatting problem:

file000xx0.122 8.62 1318.0 4.200
file00x.112 8.62 6610.2 4.200
file0C000Yxxxx.121 8.62 186.8 4.200

The formatted output should have the last 3 columns right-adjusted--
and properly aligned.

I realize that this could be done all by a pure awk script, but I have
a 'little' problem defining only the last line in each record--and
have dealt with this issue in other, similar instances.

z.entropic

Ed Morton

unread,
Jul 30, 2008, 10:14:35 AM7/30/08
to

On 7/30/2008 8:46 AM, z.entropic wrote:
> I can't figure out how to force the awk output to begin in a certain
> column after the file name is printed by a separate column, to have a
> nicely aligned output. I've tried piping through fmt and sed 's/ /\t/
> g', but nothing does what I want...
>
> The one-liner script that prints selected fields on the last line of
> each record in temp is:
>
> for i in temp/*;do print -n $i" ";tail -1 $i|awk '{printf "%2.2f %4.1f
> %1.3f\n", $4/3600/24, $8*1e6, $9}';done
>
> The following example illustrates the formatting problem:
>
> file000xx0.122 8.62 1318.0 4.200
> file00x.112 8.62 6610.2 4.200
> file0C000Yxxxx.121 8.62 186.8 4.200
>
> The formatted output should have the last 3 columns right-adjusted--
> and properly aligned.

I don't see how it's possible to get the above output given the printf formats
you used. Look:

$ printf "8.62 1318.0 4.200" | awk '{printf "%2.2f %4.1f %1.3f\n",$1,$2,$3}'
8.62 1318.0 4.200

If you want more space per field right-justfified, just increase the first
values in the format, e.g.:

$ printf "8.62 1318.0 4.200" | awk '{printf "%10.2f %10.1f %10.3f\n",$1,$2,$3}'
8.62 1318.0 4.200

> I realize that this could be done all by a pure awk script, but I have
> a 'little' problem defining only the last line in each record--and
> have dealt with this issue in other, similar instances.

To print just the last line from each file in awk, assuming no empty files,
would be:

awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}' temp/*

Ed.

z.entropic

unread,
Jul 30, 2008, 11:30:32 AM7/30/08
to
On Jul 30, 10:14 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 7/30/2008 8:46 AM, z.entropic wrote:
>
> I don't see how it's possible to get the above output given the printf formats
> you used. Look:
>
> $ printf "8.62 1318.0 4.200" | awk '{printf "%2.2f %4.1f %1.3f\n",$1,$2,$3}'
> 8.62 1318.0 4.200
>
> > I realize that this could be done all by a pure awk script, but I have
> > a 'little' problem defining only the last line in each record--and
> > have dealt with this issue in other, similar instances.
>
> To print just the last line from each file in awk, assuming no empty files,
> would be:
>
> awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}' temp/*
>
>         Ed.- Hide quoted text -
>
> - Show quoted text -

I don't know what to say; I'm using MKS ToolKit utilities in a WinXP
DOS box. Again, here is a raw output given by the followng script:

for i in temp/*;do print -n $i" ";tail -1 $i|awk '{printf "%6.2f %4.1f


%1.3f\n", $4/3600/24, $8*1e6, $9}';done

temp/V60C.122 8.62 1318.0 4.200
temp/6h_10d42V60C.112 8.62 6610.2 4.200
temp/C.121 8.62 186.8 4.200
temp/V60.111 5.80 28.8 4.200
temp/h_10d42V60.115 5.80 28.6 4.200
temp/h_10d42V60.110 5.80 32.0 4.200
temp/d42V60.114 5.80 14.0 4.200
temp/d42V60.127 5.80 165.1 4.200

I have not changed any spaces or alignment. Frankly, with two separate
output streams (print $i and awk), I'm not sure if the alignemnt can
even be done if the first filed has a variable length...

> To print just the last line from each file in awk, assuming no empty files,
> would be:
>
> awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}' temp/*
>
>
> Ed.

Thanks; that works well.

z.e.

z.entropic

unread,
Jul 30, 2008, 11:51:25 AM7/30/08
to
On Jul 30, 11:30 am, "z.entropic" <subPla...@excite.com> wrote:

> > To print just the last line from each file in awk, assuming no empty files,
> > would be:
>
> > awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}' temp/*
>
> >        Ed.
>
> Thanks; that works well.
>

> z.e.- Hide quoted text -


>
> - Show quoted text -

I was finally able to do what I wanted by a bit complex

$ awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}'
temp/*|sed 's/:/ /'|awk '{printf "%53s %5.2f
%7.1f %6.3f\n", $1, $5/3600/24, $9*1e6, $10}'

Is there a simpler way to roll the last awk formatting expression into
the first one {print lr}?

Also, I believe my output looked strange, because the filed width in
the original printf expression was specified too narrow and the output
was outmatically padded by awk.

z.e.

Ed Morton

unread,
Jul 30, 2008, 3:53:47 PM7/30/08
to
On 7/30/2008 10:51 AM, z.entropic wrote:
<snip>

> I was finally able to do what I wanted by a bit complex
>
> $ awk 'FNR==1 && NR!=FNR{print lr} {lr=FILENAME":"$0} END{print lr}'
> temp/*|sed 's/:/ /'|awk '{printf "%53s %5.2f
> %7.1f %6.3f\n", $1, $5/3600/24, $9*1e6, $10}'
>
> Is there a simpler way to roll the last awk formatting expression into
> the first one {print lr}?

If I understand what you want correctly, just change the assignment to lr:

awk 'FNR==1 && NR!=FNR{print lr}

{lr=sprintf("%53s %5.2f %7.1f %6.3f\n", $1, $5/3600/24, $9*1e6, $10)}

Hermann Peifer

unread,
Jul 30, 2008, 4:16:21 PM7/30/08
to
Ed Morton wrote:
>
> ... assuming no empty files,...
>

What about files with exactly 1 record

?

Hermann


Ed Morton

unread,
Jul 30, 2008, 5:01:34 PM7/30/08
to

On 7/30/2008 3:16 PM, Hermann Peifer wrote:
> Ed Morton wrote:
>
>>... assuming no empty files,...
>>
>
>
> What about files with exactly 1 record

Should work just fine.

Ed.

0 new messages