Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Apr 2007 19:54:29 -0700
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: [OT] can sed handle this situation? (might require variable)
Message-ID:  <462193E5.8030605@u.washington.edu>
In-Reply-To: <1176603461.20274.12.camel@joe.realss.com>
References:  <1176603461.20274.12.camel@joe.realss.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?462193E5.8030605>