awk question: replacing "%d%s" by "%d %s"
Robert Bonomi
bonomi at mail.r-bonomi.com
Fri Jan 14 00:17:00 UTC 2011
> Date: Thu, 13 Jan 2011 06:28:19 +0100
> From: Polytropon <freebsd at 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'.
More information about the freebsd-questions
mailing list