From owner-freebsd-questions@FreeBSD.ORG Thu Jan 13 06:08:23 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F2A88106564A for ; Thu, 13 Jan 2011 06:08:23 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) by mx1.freebsd.org (Postfix) with ESMTP id B3EC98FC12 for ; Thu, 13 Jan 2011 06:08:23 +0000 (UTC) Received: from r55.edvax.de (port-92-195-143-131.dynamic.qsc.de [92.195.143.131]) by mx01.qsc.de (Postfix) with ESMTP id AB5C43D24F; Thu, 13 Jan 2011 07:08:22 +0100 (CET) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id p0D68LWD005837; Thu, 13 Jan 2011 07:08:21 +0100 (CET) (envelope-from freebsd@edvax.de) Date: Thu, 13 Jan 2011 07:08:21 +0100 From: Polytropon To: Tom Limoncelli Message-Id: <20110113070821.cb2030bd.freebsd@edvax.de> In-Reply-To: References: <20110113062819.4ecb89d9.freebsd@edvax.de> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Questions Subject: Re: awk question: replacing "%d%s" by "%d %s" X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Jan 2011 06:08:24 -0000 On Thu, 13 Jan 2011 01:00:17 -0500, Tom Limoncelli 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, ...