Re: text processing problem with bash/perl

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



2009/2/13 Robinson Tiemuqinke <hahaha_30k@xxxxxxxxx>:
> 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
> } */

It's pretty simple in Perl. Something like this will work.

#!/usr/bin/perl
use strict;
use warnings;

# Put Perl in "paragraph mode"
$/ = '';

# For each record ...
while (<>) {
  # ... if the record is for localhost ...
  if (/host=localhost/) {
    # ... remove the opening comment ...
    s|/\*\s*||;
    # ... remove the closing comment ...
    s|\s*\*/||;
  }

  # ... and print the record.
  print;
}

It's written as a Unix filter, so it reads from STDIN and writes to
STDOUT. Call it like this:

  $ my_filter < input.txt > output.txt


hth,

Dave...
_______________________________________________
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