Re: How can I replace some words 1-5 lines after the matched line ?

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

 



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




[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux