formatting columns in an awk output

This is a discussion on formatting columns in an awk output within the awk forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-30-2008, 09:46 AM
z.entropic
Guest
 
Default formatting columns in an awk output

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
Reply With Quote
  #2  
Old 07-30-2008, 10:14 AM
Ed Morton
Guest
 
Default Re: formatting columns in an awk output



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.

Reply With Quote
  #3  
Old 07-30-2008, 11:30 AM
z.entropic
Guest
 
Default Re: formatting columns in an awk output

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.
Reply With Quote
  #4  
Old 07-30-2008, 11:51 AM
z.entropic
Guest
 
Default Re: formatting columns in an awk output

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.
Reply With Quote
  #5  
Old 07-30-2008, 03:53 PM
Ed Morton
Guest
 
Default Re: formatting columns in an awk output

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)}
END{print lr}' temp/*

Ed.


Reply With Quote
  #6  
Old 07-30-2008, 04:16 PM
Hermann Peifer
Guest
 
Default Re: formatting columns in an awk output

Ed Morton wrote:
>
> ... assuming no empty files,...
>


What about files with exactly 1 record

?

Hermann


Reply With Quote
  #7  
Old 07-30-2008, 05:01 PM
Ed Morton
Guest
 
Default Re: formatting columns in an awk output



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.


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:06 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.