Date: Thu, 13 Jan 2011 01:00:17 -0500 From: Tom Limoncelli <tal@whatexit.org> To: Polytropon <freebsd@edvax.de> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: awk question: replacing "%d%s" by "%d %s" Message-ID: <AANLkTi=6rOygs=ekVPAmq70hi2Lvxte7zz3SeOZuRwHf@mail.gmail.com> In-Reply-To: <20110113062819.4ecb89d9.freebsd@edvax.de> References: <20110113062819.4ecb89d9.freebsd@edvax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 13, 2011 at 12:28 AM, Polytropon <freebsd@edvax.de> wrote: > I have strings of the form either "<number(s)>" or > "<number(s)><letter>". I catch them with ... > 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 This feels like it could be faster, but is reasonable: $ cat data.txt 1 12 3 1d 1dc 12d 12dc 123d 123dc 123dcb $ cat control.txt 1 12 3 1 d 1 dc 12 d 12 dc 123 d 123 dc 123 dcb $ awk < data.txt > experiment.txt '{ num =3D $1 ; sub(/[^0-9]+$/, "", num) ; lets =3D $1 ; sub(/^[0-9]+/, "", lets); print num " " lets }' ; diff -cw control.txt experiment.txt $ # The above puts a space at the end of the first 3 lines. If that is bad, try: $ awk < data.txt > experiment.txt '{ num =3D $1 ; sub(/[^0-9]+$/, "", num) ; lets =3D $1 ; sub(/^[0-9]+/, "", lets); if (length(lets)) { print num " " lets } else { print num } }' ; diff control.txt experiment.txt $ Tom --=20 http://EverythingSysadmin.com=A0 -- my blog (new posts Mon and Wed) http://www.TomOnTime.com -- my advice (more videos coming soon)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTi=6rOygs=ekVPAmq70hi2Lvxte7zz3SeOZuRwHf>