#!perl -w use strict; while (<>) { my @words = split /\W+/; pop @words if $words[-1] eq ''; # pop @words unless $words[-1]; shift @words if $words[0] eq ''; # shift @words unless $words[0]; print $words[0], ' ', $words[-1], $/; }