Question

For example text file:

    Speak friend and enter

using a Perl script to remove whitespace and replace with carriage-return

    Speak
    friend 
    and
    enter
Was it helpful?

Solution

perl -p -e 's/\s+/\n/g'

OTHER TIPS

create a file test.pl:

open my $hfile, $ARGV[0] or die "Can't open $ARGV[0] for reading: $!";
while( my $line = <$hfile> )
{
    $line =~ s/\s+/\n/g;
    print $line;
}
close $hfile;

then run it like:

perl test.pl yourfile.txt

or, if you don't want to use a file, you can do it all from the command line like:

perl -p -e "s/\s+/\n/g" yourfile.txt
#!/usr/bin/perl -l

use strict;
use warnings;

print join "\n", split while <>;

You can use sed

sed -e "s/[ ]/\n/g"

or anything that works with regular expressions

"s/[ ]/\n/g"

If you want inplace editing you can use the -i switch. Check out perlrun to see how it's done, but, basically:

perl -p -i.bak -e 's/\s+/\n/g'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top