Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Dec 2014 12:38:24 -0600 (CST)
From:      Robert Bonomi <bonomi@mail.r-bonomi.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: Perl rename utility assistance
Message-ID:  <201412171838.sBHIcOht031153@host203.r-bonomi.com>
In-Reply-To: <54907B51.1060807@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> From owner-freebsd-questions@freebsd.org  Tue Dec 16 12:27:31 2014
> Date: Tue, 16 Dec 2014 10:34:57 -0800
> From: Jungle Boogie <jungleboogie0@gmail.com>
> Subject: Perl rename utility assistance
>
> Hello All,
>
> I figure I would try my luck here with this question. Usually I go to 
> stackoverflow for these things so pardon the noise if you're not accustom to 
> seeing letters like this.
>
> Objective: remove an underscore from a filename using rename[0].
> Example: 8213_freebsd_is_cool_Nov_2014.pdf
>
> I only want to remove the underscore (_) between the 3 and f to make it:
> 8213 freebsd_is_cool_Nov_2014.pdf
> or
> 8213 _freebsd_is_cool_Nov_2014.pdf
>
> rename '-sr/^([:digit:]_) /^[:digit:] /g' *
> rename '-s/^[:digit:]_/^[:digit:] /g' *
> rename '-sr/^[:digit:]_/^[:digit:] /g' *
>
> Doesn't return any errors but the files don't get updated either.

Of course they don't.  It did _exactly_ what you told it to.  <grin>

Your regex "^[:digit:]_" says:
           "^"             start at the beginning of the name
            "[:digit:]"    match one character from the following list ':digt'
                     "_"   match an underscore

For some strange reason, this does not cause a match where there are leading
digits, .

to match a single character in the range '0'-'9', the syntax is '[[digit:]]'

Then, you need to follow that '[[:digit:]]' with a '+' -- to add 
	'require one or more occurances of the previous match'
to the incantation, in order to match what you're looking for.

Secondly, you do *NOT* want the '^' in the replacement string.
	or the string '[:digit:]'
	these will be used _literally_ to replace the matched text,
	and you'll end up with a file whose name starts with '^[:digit:] '
	with whatever was after the original '_' concatenated to that.

Lastly, when the pattern is 'anchored' to either end of the string being 
searched, the 'g' switch is (almost always) surperfluous (there's only one
match), or leads to an infinite-recursion replacement.  It is a safe rule-
of-thumb to say "never use the 'g' switch on an anchored search (unless you
are absolutely certain what will happen on _all_ possible inputs)"  I can
construct a pathological 'hypothetical' where the 'g' switch does something
constructive on an anchored search, but I've _never_ seen a real-world 
instance, in nearly 40 years experience.


To transform '{foo}something{bar}' into "{foo}changed{bar}", you have to use
a parenthesized sub-expression in the match regex, and a backslash reference
to it in the replacement string.

Applying _all_ those changes, you end up with something like:
  rename '-s/^([[:digit:]]+)_/\1 /' *




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201412171838.sBHIcOht031153>