awk question: replacing "%d%s" by "%d %s"

Wayne Sierke ws at au.dyndns.ws
Fri Jan 14 07:38:26 UTC 2011


On Fri, 2011-01-14 at 07:17 +0100, Polytropon wrote:
> On Thu, 13 Jan 2011 18:22:18 -0600 (CST), Robert Bonomi <bonomi at mail.r-bonomi.com> wrote:
> > 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'.
> 
> Doesn't work on my 7-STABLE system (20080811), awk stops:
> 
> awk: syntax error at source line 78 source file konvertieren.awk
>  context is
>                 sub(nr, "[a-z]", " >>>  &" <<< );
> awk: illegal statement at source line 79 source file konvertieren.awk
> 
> But I'll keep your suggestion in the program source and test
> it on 8 as soon as my "new" home system is ready to use. At
> least, the & variant looks much better.

I suspect it is a transcription error by Robert in his email.

>From man awk:

       sub(r, t, s)
              substitutes t for the first occurrence of the regular
expression
              r in the string s.  If s is not given, $0 is used.


So the correct syntax is:

        sub("[a-z]", " &", nr)


Wayne




More information about the freebsd-questions mailing list