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