From owner-freebsd-questions@FreeBSD.ORG Sun Nov 30 19:25:35 2008 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 E2CDA1065670 for ; Sun, 30 Nov 2008 19:25:35 +0000 (UTC) (envelope-from drew@mykitchentable.net) Received: from smtpauth.surewest.net (smtpauth.surewest.net [66.60.130.153]) by mx1.freebsd.org (Postfix) with ESMTP id CA55F8FC14 for ; Sun, 30 Nov 2008 19:25:35 +0000 (UTC) (envelope-from drew@mykitchentable.net) Received: from blacklamb.mykitchentable.net (unknown [69.62.230.77]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtpauth.surewest.net (Postfix) with ESMTP id 07DC89BF82; Sun, 30 Nov 2008 11:25:09 -0800 (PST) Received: from [192.168.1.3] (bigdaddy.mykitchentable.net [192.168.1.3]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by blacklamb.mykitchentable.net (Postfix) with ESMTPSA id 4C050164D88; Sun, 30 Nov 2008 11:25:33 -0800 (PST) Message-ID: <4932E8A2.6030804@mykitchentable.net> Date: Sun, 30 Nov 2008 11:25:22 -0800 From: Drew Tomlinson User-Agent: Thunderbird 2.0.0.18 (Windows/20081105) MIME-Version: 1.0 To: Matthew Seaman References: <4932CA0D.8080109@mykitchentable.net> <4932D91F.5040804@infracaninophile.co.uk> In-Reply-To: <4932D91F.5040804@infracaninophile.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: FreeBSD Questions Subject: Re: Regular Expression Help 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: Sun, 30 Nov 2008 19:25:36 -0000 Matthew Seaman wrote: > Drew Tomlinson wrote: >> I'm attempting to take an ldiff file and flip first/last name order. >> However I can not figure out how to match hyphenated last names. In >> vim, my current search/replace string is: >> >> %s/cn=\(\w\+\-*\) \(\w\+\),/cn=\2 \1,/gc >> >> This will match: >> >> cn=Smith Joe, >> >> and replace it with: >> >> cn=Joe Smith, >> >> But it will not match: >> >> cn=Smith-Brown Joe, >> >> nor replace it with: >> >> cn=Joe Smith-Brown, >> >> I've tried various incantations of escaping and quantifying yet I can >> not figure out how to do what I want. > > Well, assuming that none of the surnames contain ',' and that the > first ' ' > (space) is always the delimiter between the surname and that anything > else > is forenames ... > % cat foo.txt cn=Smith Joe, > cn=Smith-Brown Joe, > % perl -p -e 's/cn=([^ ,]+) ([^,]+),/cn=$2 $1,/' < foo.txt cn=Joe Smith, > cn=Joe Smith-Brown, > > ie. you need a s/// command that understands negated character > classes. I > think sed(1) and vi(1) will do that, but I haven't time to look up the > precise > syntax. Perl, of course, just does the job for me. Thank you for your reply. The particular editor I was attempting to use is vim 7.1.315. However it doesn't like the above string so I tried you perl example verbatim. It works and even handles such lines as: cn=Smith-Brown Joe & Jane, to cn=Joe & Jane Smith-Brown, which I hadn't considered. I still don't really understand *why* the above works but I'm trying to pick it apart now. Thanks to you and the others for the replies. Cheers, Drew -- Be a Great Magician! Visit The Alchemist's Warehouse http://www.alchemistswarehouse.com