From owner-freebsd-questions@FreeBSD.ORG Thu Jan 13 06:31:28 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 E319D106566C for ; Thu, 13 Jan 2011 06:31:28 +0000 (UTC) (envelope-from tal@whatexit.org) Received: from mail-gx0-f182.google.com (mail-gx0-f182.google.com [209.85.161.182]) by mx1.freebsd.org (Postfix) with ESMTP id 9E0978FC13 for ; Thu, 13 Jan 2011 06:31:28 +0000 (UTC) Received: by gxk8 with SMTP id 8so566079gxk.13 for ; Wed, 12 Jan 2011 22:31:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=whatexit.org; s=google; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=sbFUZ5hF1ZPJ8/VPwoLjLnbLyDFlLGZqwmmUrTVBw1g=; b=DP57Eoyua99NSqxEY5drSnmKe1iaQ9LIJ0osPSJ6BDk9yyJHsZPcPCodlTKaWa6eaP SEkyhM2KVLh60uukehw/3r9EQdACcVlXcvbz69xQ71ucwFKv6g9GNPr8mc+zEjXXBwiN /0RsIPJZk7J0KWXDE0KdGIH2jozc0KLylsTw8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=whatexit.org; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=2CKPqtH4k98tCWnSKACKvh+XgT5W/DqdakDZDyc6iVA9UD/3ZwDRDwRilSAP+Y176s 5THpfy9T4Ac0294tTn5TXY6y7dftQzn9+0ExlB4yoIIlaZnscHVhnyWUy1FTBFKagBnM xDgXleJh9/yv80KnX6Z40M2Lc8HC2fE1jSq/4= MIME-Version: 1.0 Received: by 10.90.87.9 with SMTP id k9mr2708934agb.150.1294898417417; Wed, 12 Jan 2011 22:00:17 -0800 (PST) Received: by 10.90.157.8 with HTTP; Wed, 12 Jan 2011 22:00:17 -0800 (PST) In-Reply-To: <20110113062819.4ecb89d9.freebsd@edvax.de> References: <20110113062819.4ecb89d9.freebsd@edvax.de> Date: Thu, 13 Jan 2011 01:00:17 -0500 Message-ID: From: Tom Limoncelli To: Polytropon Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Jan 2011 06:31:29 -0000 On Thu, Jan 13, 2011 at 12:28 AM, Polytropon wrote: > I have strings of the form either "" or > "". I catch them with ... > where "nr" is the name of the string. What I need > is a simple space between and , > 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)