From owner-freebsd-questions@FreeBSD.ORG Sun Nov 2 11:50:42 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B4292106567B for ; Sun, 2 Nov 2008 11:50:42 +0000 (UTC) (envelope-from jpaetzel@FreeBSD.org) Received: from mail.tcbug.org (mail.tcbug.org [216.243.150.78]) by mx1.freebsd.org (Postfix) with ESMTP id 9089D8FC0C for ; Sun, 2 Nov 2008 11:50:42 +0000 (UTC) (envelope-from jpaetzel@FreeBSD.org) Received: from roadrash.tcbug.org (c-24-118-145-206.hsd1.mn.comcast.net [24.118.145.206]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.tcbug.org (Postfix) with ESMTPSA id 8FD74169E4D0; Sun, 2 Nov 2008 05:50:16 -0600 (CST) Message-ID: <490D9411.9070002@FreeBSD.org> Date: Sun, 02 Nov 2008 05:50:41 -0600 From: Josh Paetzel User-Agent: Thunderbird 2.0.0.17 (Macintosh/20080914) MIME-Version: 1.0 To: David Allen References: <2daa8b4e0811011821q210ae3a5j8f612a0fc79e8844@mail.gmail.com> <20081102045414.GA13745@shepherd> <2daa8b4e0811012210k40e92816ydbc96e36abe9ee5@mail.gmail.com> In-Reply-To: <2daa8b4e0811012210k40e92816ydbc96e36abe9ee5@mail.gmail.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: "freebsd-questions@freebsd.org" , Sahil Tandon Subject: Re: OT: Shell Script using Awk X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Nov 2008 11:50:42 -0000 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 David Allen wrote: > On 11/1/08, Sahil Tandon wrote: >> David Allen 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. Just in case you've never discovered column, piping the output of this to column -t will get you nice formatting for free. - -- Thanks, Josh Paetzel PGP: 8A48 EF36 5E9F 4EDA 5ABC 11B4 26F9 01F1 27AF AECB -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkkNlBEACgkQJvkB8Sevrsv6lwCdHk5llGh4ZG+0CnQLARJDqGD9 0AEAniRtmjDNfKXHdsGAudA3uiwYFB9f =IImT -----END PGP SIGNATURE-----