hello Aldebaran,

you can avoid the oneliner BEGIN block using the Maori-farewell like in perl -M'5; $counter=0' -nle ...

But you can avoid the counter at all:

>perl -nlE "say sprintf qq(caption%03s.txt),$count++" one.txt two.txt + three.txt caption000.txt caption001.txt caption002.txt

PS you get the filename currently processed in $ARGV so you can play with it:

>perl -nlE "say sprintf q(caption%03s.txt),$1 if $ARGV =~ /.*([\d]+).* +/" one01.txt one02.txt caption001.txt caption002.txt

PPS note that perl -n and perl -p both skip empty files!

ls -l -rw-rw-r-- 1 io io 0 feb 2 13:28 one.txt -rw-rw-r-- 1 io io 0 feb 2 13:28 three.txt -rw-rw-r-- 1 io io 20 feb 2 13:37 two.txt perl -nlE 'say "processed $ARGV" if eof' *.txt processed two.txt

PPPS after choroba's nice below response I cant resist to update the title and to try it:

perl -MTie::Scalar -M"5;{package SN; use parent -norequire => 'Tie::S +tdScalar';sub FETCH {print qq(Opening ${$_[0]}); ${$_[0]}}}; tie $ARG +V, SN" -nlE "say qq(\tprocessed $ARGV) if eof" 001.txt 002.txt 003.txt Opening 001.txt Opening 002.txt Opening 002.txt processed 002.txt Opening 003.txt

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Renaming all files in a directory -- perl -n -p and empty files oneliner by Discipulus
in thread Renaming all files in a directory by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.