[CentOS] text processing problem with bash/perl
David G. Miller
dave at davenjudy.org
Fri Feb 13 18:02:34 UTC 2009
Dennis Kaptain <dkaptain at yahoo.com.mx> wrote:
>> >
>> > 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
<SNIP>
A tad simpler:
#! /usr/bin/perl -w
use strict;
my $file;
open FILE, "stuff.txt" or die;
# Undefine the input record separator.
undef $/;
# Slurp the whole file in
$file = <FILE>;
close FILE;
# Pattern match on the stanza we want to uncomment and uncomment it.
You may need to play with
# the white space in the output to get the formatting you want.
$file =~ s?/*\s*udp_send_channel {\n\s*host=localhost\n\s*port =
10017\n\s*ttl = 1\n\s*} */\n?udp_send_channel {\n host=localhost\n
port = 10017\n ttl = 1\n}?;
# Write the result.
print $file;
#~~~~~~~~~~~~~~~~~End of Script~~~~~~~~~~~~~~
Cheers,
Dave
--
Politics, n. Strife of interests masquerading as a contest of principles.
-- Ambrose Bierce
More information about the CentOS
mailing list