Showing entries with tag "trim".

Found 2 entries

Command line trim()

Often I'll need to trim (remove leading and trailing whitespace) from command line output. The easiest way I've found is to pipe to a one line Perl script | perl -pE 's/^\s+|\s+$//'

This runs a replace regexp on the input, trims extraneous whitespace and outputs the cleaner version.

Leave A Reply

Perlfunc: trim()

Perl function to trim leading and trailing whitespace, borrowed from String::Util.

sub trim {
    my $s = shift();

    if (!defined($s)) {
        return undef;

    $s =~ s/^\s*//u;
    $s =~ s/\s*$//u;

    return $s;

To trim an each item in an array you can do:

@array = map { trim($_) } @array;
Leave A Reply