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

I have a DNS zone file that I parsed to just the network addresses. They look like this:

1.100.10 2.100.10 3.100.10

They are in "reverse" order and I'd like to find a Perl one-liner to put them right. I've already solved my problem with a script:

use strict; use warnings; open my $fh, "<", "zones.txt"; my @lines = <$fh>; close $fh; for (@lines) { chomp $_; my @F = split /\./, $_; print join ('.', reverse @F) . "\n" }

Output would be like:

10.100.1 10.100.2 10.100.3

I'm sure there must be a one-liner with something like "perl -ape -F/./ "stuff here" zone.txt" ... but I just can't get it work. Of course I'm on Windows, so that makes the whole single quote / double quote thing a bit tricky too.

Replies are listed 'Best First'.
Re: One liner help
by toolic (Bishop) on Oct 08, 2014 at 18:39 UTC
    Probably not short enough to be worth it, but this works for me on unix (perl v5.12.2), and I think it has a chance to work on windows, too:
    perl -anF"/\./" -E "chomp@F;say join q{.},reverse@F" zones.txt 10.100.1 10.100.2 10.100.3

      Thanks. I did it slightly modified borrowing the -l from kennethk above:

      (Windows 7 x64 / Strawberry Perl v5.18.1 MSWin32-x64-multi-thread)

      VinsWorldcomC:\Users\VinsWorldcom\tmp> perl -alnF"/\./" -e "print join + q{.}, reverse @F" zones.txt 10.100.1 10.100.2 10.100.3
        Nice. And golf it down with -E/say:
        perl -anlF"/\./" -E"say join q{.},reverse@F" zones.txt
Re: One liner help
by kennethk (Abbot) on Oct 08, 2014 at 18:16 UTC

    What happens with

    perl -lpe "join q{.}, reverse split /\./" zones.txt
    You could also
    perl -F. -lpe "join q{.}, reverse @F" zones.txt
    but I was never a fan of -a.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks, but neither of these work for me (Windows 7 x64 / Strawberry Perl v5.18.1 MSWin32-x64-multi-thread):

      VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl -lpe "join q{.}, reverse +split /\./" zones.txt 1.100.10 2.100.10 3.100.10

      They both just seem to print the file as-is.

        Stupid mistake; sorry:
        perl -lpe "$_=join q{.}, reverse split /\./" zones.txt

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.