Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 1 Nov 2008 21:10:03 -0800
From:      "David Allen" <the.real.david.allen@gmail.com>
To:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Cc:        Sahil Tandon <sahil@tandon.net>
Subject:   Re: OT: Shell Script using Awk
Message-ID:  <2daa8b4e0811012210k40e92816ydbc96e36abe9ee5@mail.gmail.com>
In-Reply-To: <20081102045414.GA13745@shepherd>
References:  <2daa8b4e0811011821q210ae3a5j8f612a0fc79e8844@mail.gmail.com> <20081102045414.GA13745@shepherd>

next in thread | previous in thread | raw e-mail | index | archive | help
On 11/1/08, Sahil Tandon <sahil@tandon.net> wrote:
> David Allen <the.real.david.allen@gmail.com> wrote:
>
>> My apologies for asking on this list, but I'm stuck without Perl and need
>> to use awk to generate a report.
>>
>> I'm working with a large data set spread across multiple files, but to
>> keep things simple, say I have A Very Long String that containing records,
>> each delimited by a single space.  I need to print those records in
>> columnar format, but with only 7 columns per line:
>>
>> record1  record2  record3  record4  record5  record6  record7
>> record08 record09 record10 record11 record12 record13 record14
>> ...
>
> A small sh script:
>
> #!/bin/sh
> awk ' {
> for (i=1; i<=NF; i++) {
>  printf("%s ", $i)
>  if (i % 7 == 0) { printf("\n") }
> }
> if (NF % 7 != 0) { printf("\n") }
> } ' input

An elegant solution if ever I read one.  The mod operator should have
been the first thing that came to mind.

I'm not sure whether I need a class in remedial math, or remedial awk,
but either way, my thanks for the solution.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?2daa8b4e0811012210k40e92816ydbc96e36abe9ee5>