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

I have a historical set of log files, that have the source IP at the end, rather than at the beginning. Due the the user agents being a string that can contain any character (spaces tabs, anything) , there is no set field count for the last field. I came up with the following shell script to convert them, but it is painfully slow, and thought converting it to perl might make it faster, but I'm stuck as all the examples in perl I've found expect a known number of fields.
while read line do SOURCEIP=`echo $line| sed 's/.*" "//g'|sed 's/"//g'` MEAT=`echo $line|cut -d" " -f2-` echo $SOURCEIP $MEAT >>${LOG}.txt done<$LOG
can anyone suggest a way of doing this in perl?

Replies are listed 'Best First'.
Re: reformat log files
by choroba (Cardinal) on Aug 31, 2012 at 12:51 UTC
    Can you show a sample line from the log? If it is just anything followed by a space and the ip, this should be enough:
    perl -i~ -pe ' s/(.*) ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) +$/$2 $1/;' 2001-01-02.log
    I used the following data for testing:
    234.234.234.234.asd.f2.35.346t.sdfg.aws.5332.6t.sdf.as4.23.4234 12.12. +153.43
    BTW, this is a shorter version of the regex: s/(.*) ((?:[0-9]{1,3}\.){3}[0-9]{1,3})$/$2 $1/
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      thank you. That seems to work!
Re: reformat log files
by philiprbrenan (Monk) on Aug 31, 2012 at 13:25 UTC

    If the ip address at the end is separated by a known character from the rest of the line, say a space, then:

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my $s = ' '; # Separator say s/\A(.*)$s(.+)\Z/$2$s$1/r for split /\n/, <<'END'; djshajfh kjwhjhfh aks81368`17`tr `" " \ 12.123.12.001 fueiwqgfh 9ur`023456789-ds 10.1.999.002 fdfhfsdh she eq3435235b ^&*(O&*()^&*( 10.20.30.40 fdfhfsd1111.11.11.11hheq3435235b ^&*(O&*()^&*( 1.2.3.4 fdfhfsdhheq341111.3333.4444.11.2235235b ^&*(O&*()^&*( 111.222.333.444 END

    Produces

    12.123.12.001 djshajfh kjwhjhfh aks81368`17`tr `" " \
    10.1.999.002 fueiwqgfh 9ur`023456789-ds
    10.20.30.40 fdfhfsdh she  eq3435235b ^&*(O&*()^&*(
    1.2.3.4 fdfhfsd1111.11.11.11hheq3435235b ^&*(O&*()^&*(
    111.222.333.444 fdfhfsdhheq341111.3333.4444.11.2235235b ^&*(O&*()^&*(