Re: text processing problem with bash/perl

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]




> 
> Hi,
> 
> Anyone has some ways for the following text processing problem? I have a text 
> file containing two stanzas attached below. I want to uncomment the stanza with 
> 'host=localhost' line, while left the other stanza unchanged.
> 
> ...
> 
> /* udp_send_channel {
>   host=localhost
>   port = 10017
>   ttl = 1
> } */
> 
> /* udp_send_channel {
>   host=ganglia100.ec2.example.com
>   port = 10017
>   ttl = 1
> } */
> 
> ...
> 
> If I use command below then both stanza will be altered... Please help.
> 
> sed  -i -e '/^\/\* udp_send_channel/, /} \*\// {s/^\/\* 
> udp_send_channel/udp_send_channel/g; s/\} \*\//}/g; }'
> 
> --David
> 

this is probably WAY more than you wanted but it does work, save your 2 stanzas as 'file' and run this program:

#! /usr/bin/perl -w
use strict;

open FILE,"file" or die;

my $stanzaFlag = 0;
my @buffer = ();
my ($x, $i);

while (<FILE>) {
   $i = $_;
   # see if this line has /* if yes, start saving the stanza in buffer
   if (index ($i,'/*') >= 0) {
      $stanzaFlag = 1;
   }
   # put it into the buffer
   if ($stanzaFlag == 1) {
      $buffer[$x++] .= $i;
   }
   # see if we are done with this stanza
   if (index ($i,'*/') >= 0) {
      $stanzaFlag = 0;
      # get rid of the comments.
      if (index($buffer[1],'localhost') >= 0) {
         $buffer[0] =~ s/\/\*//;
         $buffer[4] =~ s/\*\///;
      }
      print "@buffer\n";
      @buffer = ();
      $x = 0;
   }
}

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
Regístrate ya - http://correo.yahoo.com.mx/ 
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux