From owner-freebsd-questions@FreeBSD.ORG Fri Jan 14 00:17:00 2011 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 AA7B61065673 for ; Fri, 14 Jan 2011 00:17:00 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (host-120.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id 80BAF8FC16 for ; Fri, 14 Jan 2011 00:17:00 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id p0E0MIW8029158; Thu, 13 Jan 2011 18:22:18 -0600 (CST) Date: Thu, 13 Jan 2011 18:22:18 -0600 (CST) From: Robert Bonomi Message-Id: <201101140022.p0E0MIW8029158@mail.r-bonomi.com> To: freebsd-questions@freebsd.org, freebsd@edvax.de In-Reply-To: <20110113062819.4ecb89d9.freebsd@edvax.de> Cc: Subject: Re: awk question: replacing "%d%s" by "%d %s" 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: Fri, 14 Jan 2011 00:17:00 -0000 > Date: Thu, 13 Jan 2011 06:28:19 +0100 > From: Polytropon > Subject: awk question: replacing "%d%s" by "%d %s" > > I'm aware that this is not an awk question list, but I'm confident there > are many awk gurus here who can surely help me with such a stupid > problem. I also know that I get more and more stupid myself for NOT being > able to solve this, even after... some nearly infinite time. :-) > > I have strings of the form either "" or > "". I catch them with > > if(match(nr, "[a-z]")) > ... > > where "nr" is the name of the string. What I need is a simple space > between and , so for example "12a" would get "12 a", > "6d" would get "6 d", and "58" would stay unchanged. I've tried with > split(), with array manipulation and could produce 10 lines of code that > didn't work as intended > (it produced "1122aa", "66dd" and "5588" according > to the examples above). > Obviously, sub(nr, "[a-z]", " [a-z]"); is nonsense. > True. But sub(nr,"[a-z]"," &"); does the trick. (tested on Freebsd 7.2) Explamation: "&" is a 'replacement side' magic incantation to the regex library that means 'that which was matched by the pattern regex'.