http://www.perlmonks.org?node_id=826543

ddrew78 has asked for the wisdom of the Perl Monks concerning the following question:

I have a file that looks something like this:
ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252
what I'm trying to do is find each line that begins with 'ip address' and copy it directly below, so it looks like this:
ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip address 1.1.1.1 255.255.255.252 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252 ip address 2.2.2.2 255.255.255.252
I've been searching the web over, but all everybody wants to do is get rid of duplicates. Any ideas? And no, this is not homework, this is a script I'm trying to write to run in our network...I hav the rest of it done, but can't seem to be able to duplicate those lines. Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: How to duplicate lines
by jwkrahn (Abbot) on Mar 03, 2010 at 22:17 UTC
    $ echo "ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252" | perl -pe'$_ .= $_ if /^ip addres +s/' ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip address 1.1.1.1 255.255.255.252 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252 ip address 2.2.2.2 255.255.255.252
      Thanks to everyone for the quick responses. However, I made a crucial mistake when I showed what it needs to become. What I actually need to change it to needs to be
      ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip address 1.1.1.1 255.255.255.100 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252 ip address 2.2.2.2 255.255.255.100
      The last part of the duplicated line needs to change as it is being copied. Is there any way of doing this?
        Change the code given above to:

        perl -pe'$_ .= $_, s/252$/100/  if /^ip address/'

Re: How to duplicate lines
by jrsimmon (Hermit) on Mar 03, 2010 at 22:23 UTC
    use strict; my $pattern = 'ip address'; open IN_FILE, '<', 'some_file' or die $!; open OUT_FILE, '>', 'some_other_file' or die $!; while(my $line = <FILE>){ print OUT_FILE $line; print OUT_FILE $line if $line =~ /^$pattern/; } close(IN_FILE); close(OUT_FILE);
Re: How to duplicate lines
by BrowserUk (Patriarch) on Mar 03, 2010 at 22:32 UTC
    perl -pe"print if /address/" in >out

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to duplicate lines
by ungalnanban (Pilgrim) on Mar 04, 2010 at 04:49 UTC
    See the following code:
    it will duplicate the lines starting with "ip address"
    use strict; use warnings; open(FH,"data"); foreach(<FH>) { print $_; if ( $_ =~ m/^ip address/) {print $_;} }

    --sugumar--
Re: How to duplicate lines
by bichonfrise74 (Vicar) on Mar 04, 2010 at 00:06 UTC
    Something like this?
    #!/usr/bin/perl use strict; while (my $line = <DATA>) { print $line; if ( $line =~ /\sip address/ ) { $line =~ s/(\d+.\d+.\d+.)252/${1}100/; print $line; } } __DATA__ ip vrf forwarding ABC:Defgh ip address 1.1.1.1 255.255.255.252 ip vrf forwarding ABD: Efghi ip address 2.2.2.2 255.255.255.252
      Great, thank you so much. That did the trick.