From owner-freebsd-questions@FreeBSD.ORG Sat Nov 8 22:02:22 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 7C16A1065679 for ; Sat, 8 Nov 2008 22:02:22 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id D0CF48FC1A for ; Sat, 8 Nov 2008 22:02:21 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (adsl202-73.kln.forthnet.gr [79.103.15.73]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-5) with ESMTP id mA8M2DJO012310 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sun, 9 Nov 2008 00:02:19 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id mA8M2DMH066505; Sun, 9 Nov 2008 00:02:13 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id mA8M2BcH066445; Sun, 9 Nov 2008 00:02:11 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: bsd References: <4B1A9F30-B8BC-4C48-A85F-3697C6AB3B7B@todoo.biz> Date: Sun, 09 Nov 2008 00:02:11 +0200 In-Reply-To: <4B1A9F30-B8BC-4C48-A85F-3697C6AB3B7B@todoo.biz> (bsd@todoo.biz's message of "Sat, 8 Nov 2008 19:43:52 +0100") Message-ID: <87skq1yizg.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-MailScanner-ID: mA8M2DJO012310 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-4.058, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.34, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: Liste FreeBSD Subject: Re: scripting text replacement 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: Sat, 08 Nov 2008 22:02:22 -0000 On Sat, 8 Nov 2008 19:43:52 +0100, bsd wrote: > Sorry for this cross posting, but I can not find a good "bash" mailing > list=E2=80=A6 > I am certain FreeBSD gurus will provide me with a fast and reliable > answer to this little question. > > Here is the deal: > ----------------- > > I have a file containing a list of items like that: > > line1item1 line1item2 line1item3 > line2item1 line2item2 line2item3 > =E2=80=A6400 times > > I need to insert this into another text file using printf() items should > be converted into variable looping=E2=80=A6 like that: > > printf "Bla bla bla $1 bla bla $2 bla bla $3 bla bla $2" > > The main thing is that I can not get $1 $2 $3 to correspond to > line1item1 line1item2 line1item3 A little more detail about the "Bla bla" part may be important in our effort to help you effectively. What you seem to describe above may be trivial to do with awk(1): ,----------------------------------------------------------------------- | $ cat /tmp/inputfile | line1item1 line1item2 line1item3 | line2item1 line2item2 line2item3 | $ awk '{ | printf "Bla bla bla %s bla bla %s bla bla %s bla bla %s\n", | $1, $2, $3, $2; | }' /tmp/inputfile | Bla bla bla line1item1 bla bla line1item2 bla bla line1item3 bla bla line= 1item2 | Bla bla bla line2item1 bla bla line2item2 bla bla line2item3 bla bla line= 2item2 | $ `----------------------------------------------------------------------- or with a short script in sed(1) or Perl: ,----------------------------------------------------------------------- | $ perl \ | -pe 's/(\S+)\s+(\S+)\s+(\S+)/Bla bla bla $1 bla bla $2 bla bla $3 bla b= la $2/' \ | /tmp/inputfile | Bla bla bla line1item1 bla bla line1item2 bla bla line1item3 bla bla line= 1item2 | Bla bla bla line2item1 bla bla line2item2 bla bla line2item3 bla bla line= 2item2 | $ `----------------------------------------------------------------------- More complex substitutions can be scripted in almost any scripting language you prefer. HTH, Giorgos