## Substitute 2 or more horizontal white space with pipe s/[\h+]{2,}/|/g; #### #!/usr/bin/perl local $/; open my $fh, '<', 'whitespacegone.txt' or die $!; my $txt = <$fh>; $txt =~ s/(\n\|)/ /mg; for ( split '\n', $txt ) { # process each line } print $txt;