TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:18790] quick way to strip spaces...



No, the '.' character is a match for any character in a regular
expression. If you want to match a '.', it must be escaped (or put in
square brackets '[.]'). Use

	man perlre

and it will answer many Perl regular expression questions.

Have a good day,

Troy

Brian Ackermann wrote:
> 
> In the script, the line...
> s/\.MP3/.mp3/g;
> 
> is the escape on the .MP3 a typo?
> 
> > From: Troy Johnson <john1536@tc.umn.edu>
> > Reply-To: tclug-list@mn-linux.org
> > Date: Thu, 15 Jun 2000 10:26:18 -0500
> > To: tclug-list@mn-linux.org
> > Subject: Re: [TCLUG:18790] quick way to strip spaces...
> >
> > Yep, except I would put a ',' between the 0655 and the last argument to
> > chmod. And change the last argument to chmod to be $_ (it is already
> > moved).
> >
> > This is also *Untested*
> >
> >>>>>> START>>>>>
> > #!/usr/bin/perl -w
> >
> > opendir D, ".";
> > for (readdir D)
> > {
> > $oldname = $_;
> > s/[ \[\]{}<>(),~]/_/g;
> > s/\.MP3/.mp3/g;
> > rename $oldname, $_;
> > chmod 0655, $_;
> > }
> > <<<<<<END<<<<<<
> >
> >
> > Brian Ackermann wrote:
> >>
> >> My own 'untested' answer to the 'riddle'
> >>
> >> perl -e 'opendir D, "."; for(readdir D) {$x=$_; s/\s+/_/g;
> >> s/[\[\]()<>{},~]/_/g; s/MP3/mp3/g; rename $x, $_; chmod 0655 $x}'
> >>
> >> dunno if that would work at all...but it seems it might...
> >>
> >>> From: Brian Ackermann <brian_ackermann@bbros.com>
> >>> Reply-To: tclug-list@mn-linux.org
> >>> Date: Thu, 15 Jun 2000 10:17:22 -0500
> >>> To: <tclug-list@mn-linux.org>
> >>> Subject: Re: [TCLUG:18790] quick way to strip spaces...
> >>>
> >>> Ok, this one should maybe be a bit of a brain teaser for some of you out
> >>> there...I don't actually NEED the answer to the question below, I just
> >>> thought some of you might like to stretch your perl muscles a little
> >>> bit...then again, for as much as I know about perl, its probably trivial...!
> >>>
> >>> for that same directory, I want to strip out all spaces, brackets of any
> >>> kind, commas and tildes, change all .MP3 extensions to .mp3, and run a chmod
> >>> 0655 on the bunch as well....can all this be done as part of the same perl
> >>> statement, and what would it look like.
> >>>
> >>> Would it be better at this point to actually make a perl script to do it
> >>> all, instead of just continuously typing in that long line of code again and
> >>> again(say once every week or so...)
> >>>
> >>>> From: Brian Ackermann <brian_ackermann@bbros.com>
> >>>> Reply-To: tclug-list@mn-linux.org
> >>>> Date: Thu, 15 Jun 2000 09:55:03 -0500
> >>>> To: <tclug-list@mn-linux.org>
> >>>> Subject: Re: [TCLUG:18790] quick way to strip spaces...
> >>>>
> >>>> perl -e 'opendir D, "."; for(readdir D) {$x=$_; s/[(,)]/_/g; rename $x,
> >>>> $_;}'
> >>>>
> >>>> That did the trick perfectly!!  (I also had some commas to remove, so I
> >>>> added that bit in as well...)
> >>>>
> >>>>> From: Troy Johnson <john1536@tc.umn.edu>
> >>>>> Reply-To: tclug-list@mn-linux.org
> >>>>> Date: Wed, 14 Jun 2000 17:05:17 -0500
> >>>>> To: tclug-list@mn-linux.org
> >>>>> Subject: 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
> >>>>>
> >>>>> ---------------------------------------------------------------------
> >>>>> To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> >>>>> For additional commands, e-mail: tclug-list-help@mn-linux.org
> >>>>>
> >>>>>
> >>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> >>>> For additional commands, e-mail: tclug-list-help@mn-linux.org
> >>>>
> >>>>
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> >>> For additional commands, e-mail: tclug-list-help@mn-linux.org
> >>>
> >>>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> >> For additional commands, e-mail: tclug-list-help@mn-linux.org
> >
> > --
> > Troy Johnson   mailto:john1536@tc.umn.edu   http://umn.edu/~john1536/
> > A human being should be able to change a diaper, plan an invasion,
> > butcher a hog, conn a ship, design a building, write a sonnet, balance
> > accounts, build a wall, set a bone, comfort the dying, take orders, give
> > orders, cooperate, act alone, solve equations, analyze a new problem,
> > pitch manure, program a computer, cook a tasty meal, fight efficiently,
> > die gallantly. Specialization is for insects.
> > -- Robert A. Heinlein Time Enough for Love
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> > For additional commands, e-mail: tclug-list-help@mn-linux.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tclug-list-unsubscribe@mn-linux.org
> For additional commands, e-mail: tclug-list-help@mn-linux.org

-- 
Troy Johnson   mailto:john1536@tc.umn.edu   http://umn.edu/~john1536/
In the absence of any evidence tending to show that possession 
or use of a 'shotgun having a barrel of less than eighteen inches 
in length' at this time has some reasonable relationship to the 
preservation or efficiency of a well regulated militia, we cannot 
say that the Second Amendment guarantees the right to keep and bear 
such an instrument. [...] The Militia comprised all males 
physically capable of acting in concert for the common defense.  
        -- Majority Supreme Court opinion in "U.S. vs. Miller" (1939)