Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Jan 2011 18:22:18 -0600 (CST)
From:      Robert Bonomi <bonomi@mail.r-bonomi.com>
To:        freebsd-questions@freebsd.org, freebsd@edvax.de
Subject:   Re: awk question: replacing "%d%s" by "%d %s"
Message-ID:  <201101140022.p0E0MIW8029158@mail.r-bonomi.com>
In-Reply-To: <20110113062819.4ecb89d9.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help

> Date: Thu, 13 Jan 2011 06:28:19 +0100
> From: Polytropon <freebsd@edvax.de>
> 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 "<number(s)>" or
> "<number(s)><letter>". 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 <number(s)> and <letter>, 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'.





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