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) = (@_, $_); # Passed in var, or default to $_
    if (!defined($s) || length($s) == 0) { return ""; }
    $s =~ s/^\s*//;
    $s =~ s/\s*$//;

    return $s;

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

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