From owner-freebsd-questions@FreeBSD.ORG Sun Apr 15 02:54:04 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 2ECB416A406 for ; Sun, 15 Apr 2007 02:54:04 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout5.cac.washington.edu (mxout5.cac.washington.edu [140.142.32.135]) by mx1.freebsd.org (Postfix) with ESMTP id 0C64D13C457 for ; Sun, 15 Apr 2007 02:54:04 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from smtp.washington.edu (smtp.washington.edu [140.142.33.9] (may be forged)) by mxout5.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l3F2s3Pn014299 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sat, 14 Apr 2007 19:54:03 -0700 X-Auth-Received: from [192.168.10.45] (c-24-7-142-221.hsd1.ca.comcast.net [24.7.142.221]) (authenticated authid=youshi10) by smtp.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l3F2s2dc002422 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sat, 14 Apr 2007 19:54:03 -0700 Message-ID: <462193E5.8030605@u.washington.edu> Date: Sat, 14 Apr 2007 19:54:29 -0700 From: Garrett Cooper User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <1176603461.20274.12.camel@joe.realss.com> In-Reply-To: <1176603461.20274.12.camel@joe.realss.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version: 5.3.0.289146, Antispam-Engine: 2.5.0.283055, Antispam-Data: 2007.4.14.194134 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='__CP_URI_IN_BODY 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __USER_AGENT 0' Subject: Re: [OT] can sed handle this situation? (might require variable) 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, 15 Apr 2007 02:54:04 -0000 Zhang Weiwu wrote: > Dear list. I could not find a mailing list about 'sed' (there is an very > inactive Yahoo Group though) so I wish to try some luck here. Sorry for > OT. > > I've got a situation that looks like require using variable and not > possible to process with sed. But I am not sure. Can someone suggest me > if this task is out of scope of sed? > > The input document is sections of data separated by an empty new line; > in each section there are a few lines. It's like this: > > dn: uid=ABB,ou=contacts,ou=china,dc=ahk,dc=de > uid: ABB > ahkCreateTimeStamp: 19960328000000Z > creatorsName: cn=manager,dc=ahk,dc=de > createTimestamp: 20060425094550Z > > dn: uid=paulblome,ou=contacts,ou=china,dc=ahk,dc=de > uid: paulblome > sn: Blome > createTimestamp: 20060417071950Z > modifiersName: cn=manager,dc=ahk,dc=de > modifyTimestamp: 20060630094026Z > > The above sample showed two sections in input data. It's required to > process the data in following rule: > > if a data section has "ahkCreateTimeStamp: abc", replace it with > "createTimestamp: abc" and remove the original "createTimestamp: > def" line; > > That is, the result data of above sample should be: > > dn: uid=ABB,ou=contacts,ou=china,dc=ahk,dc=de > uid: ABB > createTimestamp: 19960328000000Z > creatorsName: cn=manager,dc=ahk,dc=de > > dn: uid=paulblome,ou=contacts,ou=china,dc=ahk,dc=de > uid: paulblome > sn: Blome > createTimestamp: 20060417071950Z > modifiersName: cn=manager,dc=ahk,dc=de > modifyTimestamp: 20060630094026Z > Sure, and no this doesn't require an additional variable. If my sed'ing is correct (I usually do regular expressions with Perl), the expression should be: sed -e 'm/^\s+.+createTimeStamp:.+\s+createTimeStamp: (.+)/\s{number of preceding required spaces}createTimeStamp: \1/g'; I'd be sure to test out the expression though first before replacing any files. My reference for the text replace was: http://www.student.northpark.edu/pemente/sed/sed1line.txt (look for the comma in number replace reference). Cheers, -Garrett