Thursday 21 May 2009

Perl one liner to remove new line char,leading space and trailing space

perl -e
'while (<>) {
foreach ($_) {
chomp;
s/^\s+//;
s/\s+$//;
next unless length ;
print $_."\n"
}
}' sample > sample.nospace

1 comment:

  1. Or you could use the -n perl command-line option, which does the loop for you:

    perl -ne's/^\s+//;s/\s+$//;print $_ unless length;'

    See man perlrun.

    ReplyDelete

Tweets by @sriramperumalla