Re: manipulatind file names.
From: John W. Krahn (someone_at_example.com)
Date: 04/06/05
- Next message: Alan Connor: "Re: Changing certain words in a code to lower case."
- Previous message: Peter J. Acklam: "Re: sorting words in string by length of word"
- In reply to: qazwart: "Re: manipulatind file names."
- Next in thread: qazwart: "Re: manipulatind file names."
- Reply: qazwart: "Re: manipulatind file names."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 06 Apr 2005 08:11:03 GMT
qazwart wrote:
> The Perl code above isn't all that bad if you just take a little time
> to parse it. First of all, let's put in some white space:
>
> ls -1 | perl -ne 'BEGIN
> {
> ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
> localtime(time);
> $year += 1900;
> $mon++;
> $tm = sprintf("%04d%02d%02d", $year ,$mon, $mday)
> }
>
> chomp;
> $o = $_;
> s/(.*)\.(.*)$/\1_$tm.$2/;
> print "mv $o $_\n";
> '
> Next, understand that "perl -n" assumes a "while read" loop around the
> whole program just like in AWK. The "BEGIN" is also just like in AWK
> where you can do some programming before beginning the loop.
>
> The program becomes much easier to understand when you put in the
> implied "while read" loop
>
> =========================================
> ($sec, $min, $hour, $mday, $mon, $year) = localtime #Removed variables not needed
Except that $sec, $min and $hour are not needed but you didn't remove them.
You could do that by either using undef() as a place holder:
(undef, undef, undef, $mday, $mon, $year) = localtime;
Or by using a list slice:
($mday, $mon, $year) = (localtime)[3,4,5];
Also that statement is missing a semicolon which is a syntax error.
> $year += 1900;
> $mon++;
> $time = sprintf("%04d"%02d%02d", $year, $mon, $mday);
>
> open (LS, "ls -1|") or die qq(Cannot execute "ls" command);
>
> while (<LS>)
Why not just use perl's built-in file globbing:
foreach ( <*> )
That way you don't have to fork a separate process to run 'ls'.
> {
> chomp;
> $o = $_;
> s/(.*)\.(.*)$/\1_$tm.$2/;
> print "mv $o $_\n";
> }
John
-- use Perl; program fulfillment
- Next message: Alan Connor: "Re: Changing certain words in a code to lower case."
- Previous message: Peter J. Acklam: "Re: sorting words in string by length of word"
- In reply to: qazwart: "Re: manipulatind file names."
- Next in thread: qazwart: "Re: manipulatind file names."
- Reply: qazwart: "Re: manipulatind file names."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|