TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:18790] quick way to strip spaces...
Brian Ackermann wrote:
> > From: "Eric Hillman" <ehillman@cccu.com>
> >
> > perl -e 'opendir D, "."; for(readdir D) {$x=$_; s/\s+/_/g; rename $x, $_;}'
>
> However, I'm wondering if you could explain this bit for me...
> {$x=$_; s/\s+/_/g; rename $x, $_;}
> I also am going to need to strip out '(' and ')', and I'm not seeing how to
> do that.
{$x=$_; s/\s+/_/g; s/[()]//g; rename $x, $_;}
The semicolons (';') here separate single statements.
The '$_' variable is a special default variable for loops. When 'readdir
D' produces a list of files names, the 'for' loop sends them one at a
time throught the loop and assigns the file name to the $_ variable.
'$x' here is just a variable to store the old file name for use in the
'rename' function.
The regular expression substitutions ('s/../../g') can be used to work
on variables ('$n =~ s/1/2/g; # replace ones with twos in n'), but when
no variable is given, the substitution works on the default '$_'
variable instead.
The 's/[()]//g' part is a substitution ('s') and it is global ('g'). The
'/' characters separate: the regular expression type or function ('s'),
what is matched ('[()]'), what replaces a match ('' or nothing), and the
options ('g'). The '[' and ']' delimit a set of characters to be
matched, and this one will match a '(' charcter or a ')' character. The
global ('g') option means it can match more than once per line or input
chunk.
Sorry if that seems to simplistic or complicated. I am just practicing
my written English. ;-)
Good luck, and get "Learning Perl" or "Programming Perl". You will not
regret it,
Troy
--
Troy Johnson mailto:john1536@tc.umn.edu http://umn.edu/~john1536/
Question with boldness even the existence of a God; because, if there
be one, he must more approve the homage of reason, than that of
blindfolded fear.... Do not be frightened from this inquiry from any
fear of its consequences. If it ends in the belief that there is no
God, you will find incitements to virtue in the comfort and
pleasantness you feel in its exercise...
-- Thomas Jefferson, in a 1787 letter to his nephew