Re: [OT] can sed handle this situation? (might require variable)

2007-04-15 Thread Zhang Weiwu
On Sun, 2007-04-15 at 01:40 -0400, Parv wrote:
 in message [EMAIL PROTECTED],
 wrote Zhang Weiwu thusly...
 
  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.
 
 Try, comp.unix.misc newsgroup.
 
 
  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?
 
 Try some variation of what Garret suggested if sed is the
 requirement and skip rest of the message.

Thank you very much for all of you providing insights. I have not yet
tried all possibilities in sed but I have just discovered it's
relatively easy to handle this task in awk with this script:

/^$/ { print createTimestamp:, timeStamp; timeStamp = ; }
/^ahkCreateTimeStamp:/ { timeStamp = $2;}
/^createTimestamp:/ { if (timeStamp == ) timeStamp = $2; }

$0 !~ /^ahkCreateTimeStamp:/  $0 !~ /^createTimestamp:/ {
print;
}

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] can sed handle this situation? (might require variable)

2007-04-15 Thread Parv
in message [EMAIL PROTECTED],
wrote Zhang Weiwu thusly...

 On Sun, 2007-04-15 at 01:40 -0400, Parv wrote:
  in message [EMAIL PROTECTED],
  wrote Zhang Weiwu thusly...
...
   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?
 
  Try some variation of what Garret suggested if sed is the
  requirement and skip rest of the message.

 Thank you very much for all of you providing insights. I have not
 yet tried all possibilities in sed but I have just discovered it's
 relatively easy to handle this task in awk with this script:

 /^$/ { print createTimestamp:, timeStamp; timeStamp = ; }

This prints a createTimestamp: line on its own block for every 2
consecutive empty lines (in context of the whole program).


 /^ahkCreateTimeStamp:/ { timeStamp = $2;}
 /^createTimestamp:/ { if (timeStamp == ) timeStamp = $2; }

 $0 !~ /^ahkCreateTimeStamp:/  $0 !~ /^createTimestamp:/ {
   print;
 }

Interesting, your description of the solution (sadly not in the
quoted reply) allowed for createTimestamp move to
ahkCreateTimeStamp line, but implementation above keeps the
createTimestamp at its place.  So I suppose order does not
matter(?).


  - Parv

-- 

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] can sed handle this situation? (might require variable)

2007-04-15 Thread Zhang Weiwu

Parv 写道:

in message [EMAIL PROTECTED],
wrote Zhang Weiwu thusly...
  

On Sun, 2007-04-15 at 01:40 -0400, Parv wrote:


in message [EMAIL PROTECTED],
wrote Zhang Weiwu thusly...
  

...
  

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?


Try some variation of what Garret suggested if sed is the
requirement and skip rest of the message.
  

Thank you very much for all of you providing insights. I have not
yet tried all possibilities in sed but I have just discovered it's
relatively easy to handle this task in awk with this script:

/^$/ { print createTimestamp:, timeStamp; timeStamp = ; }



This prints a createTimestamp: line on its own block for every 2
consecutive empty lines (in context of the whole program).

  
Ah, yes, if there are two empty lines this line would create a mistake. 
The original input file's sections are strictly one-empty-line-divided 
and one empty line before EOF, thus I was lucky. However I wasn't aware 
of your point when I wrote this.
  

/^ahkCreateTimeStamp:/ { timeStamp = $2;}
/^createTimestamp:/ { if (timeStamp == ) timeStamp = $2; }

$0 !~ /^ahkCreateTimeStamp:/  $0 !~ /^createTimestamp:/ {
print;
}



Interesting, your description of the solution (sadly not in the
quoted reply) allowed for createTimestamp move to
ahkCreateTimeStamp line, but implementation above keeps the
createTimestamp at its place.  So I suppose order does not
matter(?).

  

Well, actually order is not important. I forgot to mention this...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


[OT] can sed handle this situation? (might require variable)

2007-04-14 Thread Zhang Weiwu
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: 1996032800Z
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: 1996032800Z
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
-- 
Zhang Weiwu
Real Softservice
http://www.realss.com
+86 592 2091112

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] can sed handle this situation? (might require variable)

2007-04-14 Thread Garrett Cooper

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: 1996032800Z
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: 1996032800Z
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
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] can sed handle this situation? (might require variable)

2007-04-14 Thread Parv
Darn, forgot to copy to the dear list; so here it is (sent to OP
previously) ...


in message [EMAIL PROTECTED],
wrote Zhang Weiwu thusly...

 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.

Try, comp.unix.misc newsgroup.


 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?

Try some variation of what Garret suggested if sed is the
requirement and skip rest of the message.


 The input document is sections of data separated by an empty new
 line
...
dn: uid=ABB,ou=contacts,ou=china,dc=ahk,dc=de
uid: ABB
ahkCreateTimeStamp: 1996032800Z
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: 1996032800Z
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

Here is my version in Perl (v5.8.8; run it by giving it files to
process as command line arguments; no files are modified; output
goes to the standard output) ...

  #!/usr/local/bin/perl

  use warnings; use strict;

  my $orig = 'createTimestamp';
  my $changed = 'ahkCreateTimeStamp' ;

  #  Mapping of changed  original strings with related regular
  #  expressions.
  my %replacement;
  @replacement{ ( 'changed' , 'orig' ) } =
map
{ [ $_
  , qr(^ \s*#  Optional whitespace at the beginning;
$_  #  time stamp text;
\s* :   #  optional whitespace before colon;
\s* #  optional whitespace;
\S+ #  non whitespace character sequence (time stamp);
.* $#  then anything or nothing else at the end.
  )xm
  ]
}
( $changed , $orig )
;

  #  Process files, given as command line arguments.  Output is
  #  printed on standard output, no file is actually modified.
  for my $file ( @ARGV )
  {
my $fh;
unless ( open $fh , '' , $file )
{
  warn Cannot open file '$file': $!\n ;
  next;
}

update_time_stamp( \%replacement , $fh );

close $fh or die Cannot close '$file': $!\n ;
  }
  exit;

  sub update_time_stamp
  {
my ( $map , $fh ) = @_;

my $changed = $map-{'changed'};
my $orig = $map-{'orig'};

#  Set input record separator to parse data in blocks.
local $/ =  ;
while ( my $block = $fh )
{
  #  Nothing to do if there is no ahk* string.
  next
unless $block =~ m/$changed-[1]/
 $block =~ m/$orig-[1]/ ;

  for ( $block )
  {
#  Remove original replacement time stamp line.  (Order does
#  not matter as only the text is changed not the associated
#  time stamp value.)
s/$orig-[1]//;

#  Update time stamp string.
s/$changed-[0]/$orig-[0]/;
  }

  #  Remake the block by removing empty line (caused by removal of
  #  replacement time stamp line.)
  $block =
join \n
, grep { $_ !~ m/^\s*$/ } split /\n+/ , $block
;

  #  Add removed new line at the end, and another as separator.
  $block .= \n\n ;
}
#  For each  every block processed ...
continue
{
  print $block ;
}
  }
  __END__


  - Parv

-- 

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]