From owner-freebsd-questions@FreeBSD.ORG Wed Dec 17 18:46:26 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 198BBD62 for ; Wed, 17 Dec 2014 18:46:26 +0000 (UTC) Received: from host203.r-bonomi.com (host203.r-bonomi.com [204.87.227.203]) by mx1.freebsd.org (Postfix) with ESMTP id CB3648FB for ; Wed, 17 Dec 2014 18:46:25 +0000 (UTC) Received: (from bonomi@localhost) by host203.r-bonomi.com (8.14.9/8.14.7) id sBHIcOht031153; Wed, 17 Dec 2014 12:38:24 -0600 (CST) (envelope-from bonomi) Date: Wed, 17 Dec 2014 12:38:24 -0600 (CST) From: Robert Bonomi Message-Id: <201412171838.sBHIcOht031153@host203.r-bonomi.com> To: freebsd-questions@freebsd.org Subject: Re: Perl rename utility assistance In-Reply-To: <54907B51.1060807@gmail.com> X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Dec 2014 18:46:26 -0000 > 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 > 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. 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 /' *