Re: manipulatind file names.

From: John W. Krahn (someone_at_example.com)
Date: 04/06/05


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


Relevant Pages

  • Re: Initialising a hash
    ... error codes. ... External file, read and parse into a hash with the number as the ... seem to be any good reason not to write this as plain Perl code but 2. ...
    (comp.lang.perl.misc)
  • Re: Initialising a hash
    ... External file, read and parse into a hash with the number as the ... seem to be any good reason not to write this as plain Perl code but 2. ... For non-textual data or situations where multiple files are ...
    (comp.lang.perl.misc)
  • RE: monitoring corporate internet usage
    ... I used some Perl code to parse our various logs and produce a simple ... If you are unfamiliar with Perl and want to go ... Subject: monitoring corporate internet usage ... I am looking for any freeware that I could use to parse through syslog ...
    (Security-Basics)
  • help finding module
    ... I need to parse through a large amount of perl code and find functions ... I have been looking for a tool on CPAN for a while that can help me. ...
    (perl.beginners)
  • reg expression with input line
    ... I would like to write a perl script to parse each line read from a text ... I ended up some perl code as shown below: ... But the parser failed to parse the input text, ... parsing the $date. ...
    (comp.lang.perl.misc)