views:

1821

answers:

5

For example text file:

    Speak friend and enter

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

    Speak
    friend 
    and
    enter
+16  A: 
perl -p -e 's/\s+/\n/g'
Peter Kovacs
Thank you Peter, that did the trick
If it did the trick, you should accept his answer.
innaM
+2  A: 

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
arolson101
thanks arolson101, that also did the trick
+1  A: 

You can use sed

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

or anything that works with regular expressions

"s/[ ]/\n/g"
monkey_p
and thanks monkeyp, this is a friendly website
OP asked for a Perl script
justintime
+1  A: 

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'
Donato Azevedo
A: 
#!/usr/bin/perl -l

use strict;
use warnings;

print join "\n", split while <>;
Sinan Ünür
Nice. I can't understand ones which down vote this perfect answer.
Hynek -Pichi- Vychodil