How can I replace some words 1-5 lines after the matched line ?
Jason Dixon
jason at dixongroup.net
Tue Jan 20 12:35:49 UTC 2004
On Tue, 2004-01-20 at 05:29, Steven Shiau wrote:
> Hi!
> I am trying to write a script to replace some words 1-5 lines after the
> matched line. For example, here is some part in kernel-2.4.spec,
> ...
> # Second, per-architecture exclusions (ifarch)
> %ifarch i386
> %define buildsmp 0
> %define buildup 0
> %endif
> %ifarch i586
> %define buildsmp 0
> %endif
Actually, Perl is the perfect solution when working with any kind of
text manipulation. The problem is that you're viewing the problem in a
linear context, when what we really need is to simply recreate the
if/endif structure with a Perl hash. Here's an example that should get
you started. If you have problems getting it all to mesh, let me know.
#### spec file ####
%ifarch i386
%define buildsmp 0
%define buildup 0
%endif
%ifarch i586
%define buildsmp 0
%endif
###################
##### arch.pl #####
#!/usr/bin/perl
use strict;
use Data::Dumper;
my (%hash, $key);
open(IN, "arch.txt") || die "Can't open file: $!";
while (<IN>) {
next unless (/ifarch/ || /define/ || /endif/);
my $line = $_;
chomp $line;
if ($line =~ /endif/) {
next;
}
if ($line =~ /ifarch/) {
$key = (split(/ /, $line))[1];
next;
} else {
$hash{$key}{(split(/ /, $line))[1]} = (split(/ /, $line))[2]; }
}
close(IN);
print Dumper(\%hash);
###################
##### output ######
$VAR1 = {
'i386' => {
'buildup' => '0',
'buildsmp' => '0'
},
'i586' => {
'buildsmp' => '0'
}
};
###################
--
Jason Dixon, RHCE
DixonGroup Consulting
http://www.dixongroup.net
More information about the fedora-list
mailing list