Date: Thu, 13 Jan 2011 07:08:21 +0100 From: Polytropon <freebsd@edvax.de> To: Tom Limoncelli <tal@whatexit.org> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: awk question: replacing "%d%s" by "%d %s" Message-ID: <20110113070821.cb2030bd.freebsd@edvax.de> In-Reply-To: <AANLkTi=6rOygs=ekVPAmq70hi2Lvxte7zz3SeOZuRwHf@mail.gmail.com> References: <20110113062819.4ecb89d9.freebsd@edvax.de> <AANLkTi=6rOygs=ekVPAmq70hi2Lvxte7zz3SeOZuRwHf@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 13 Jan 2011 01:00:17 -0500, Tom Limoncelli <tal@whatexit.org> wrote: > $ awk < data.txt > experiment.txt '{ num = $1 ; sub(/[^0-9]+$/, "", > num) ; lets = $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 = $1 ; sub(/[^0-9]+$/, "", > num) ; lets = $1 ; sub(/^[0-9]+/, "", lets); if (length(lets)) { print > num " " lets } else { print num } }' ; diff control.txt experiment.txt > $ Thanks for your inspiration! I concluded that it is a possible way to split the string into two parts: not-numbers (letters) and numbers, and then recombining them in the desired way. Here's my code: if(match(nr, "[a-z]")) { z = nr; gsub(/[^0-9]+$/, "", z); b = nr; gsub(/[0-9]/, "", b); nr = sprintf("%s %s", z, b); } First catch all nr's that have a letter in it, then remove the letters in one copy, then the numbers in another one, and finally overwrite nr with the proper recombination that includes the space. Works as intended, FULLY. Thanks! -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110113070821.cb2030bd.freebsd>