Skip to content
Advertisement

Change file names by Perl [closed]

How to change file names from notation like this: SomeCodeFile.extension to C/C++ notation: some_code_file.extension. I’d like to use Perl + Find + Regex to print those names.

Advertisement

Answer

You can use a regular expression:

perl -e 'for $f (@ARGV) {
             ($n = $f) =~ s/(?<=.)([[:upper:]])/_l$1/g;
             rename $f, lcfirst $n;
         }' SomeCodeFiles*
  • [[:upper:]] matches upper case letters.
  • (?<=.) is a look behind assertion, i.e. the letter is preceded by something (we don’t want _some_code_file.ext)
  • l changes the following letter to lowercase (similarly to lcfirst)

Note that the result might be unpleasant in some situations (x_m_l2_j_s_o_n.c, u_r_l__validator.cpp).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement