0

I've been playing with the perl implementation of the rename tool in cygwin having finally got it working via installing from cpan.

It's been a long time since i played with this stuff so I'm a bit rusty with my regexp stuff.

basically i'm just trying to rename a bunch of folders of the form

[2022-10-10] This is a Test - This is a Test (some stuff) [393933939339]

or

[2022] This is a Test - This is a Test (some stuff) [393933939339]

and move the stuff at the start delimited by [] to the end so it looks like

This is a Test - This is a Test (some stuff) [393933939339] [2022-10-10] 

or

This is a Test - This is a Test (some stuff) [393933939339] [2022] 

I've worked out how to do it using position parameters but am struggling to do it using pattern matching for []

rename  -n 's/^(.{12})(.*)$/$2 $1/'

will do the work for the folders with the full date and obviously but not for the folders with just a year in them. Ideally I'd like a single expression to process them all.

I think it may be complicated by the fact that [] are treated as operators so may need escaping in some way.

Any help appreciated.

2 Answers 2

1

I think it may be complicated by the fact that [] are treated as operators so may need escaping in some way.

Exactly so. The standard way to escape a character in an RE is to precede it with a backslash \. So,

rename -n 's/^(\[.*?])\s*(.*)/$2 $1/s' '['*

It has been recommended that the trailing s modifier on the pattern substitution should be included, as this allows the pattern to match newlines as characters (they are valid in file names, after all). Also, restrict the set of files for consideration to be those that could be transformed (this disregards file names starting -v , etc. that would be passed by the shell and processed by rename as valid argument options) by using '['* as the filename pattern; you could use ./* too.

0
0

What I would do:

$ rename -n 's/^(\[.*?\])(.*)/$2 $1/' *393933939339*
rename([2022] This is a Test - This is a Test (some stuff) [393933939339],  This is a Test - This is a Test (some stuff) [393933939339] [2022])

The regular expression matches as follows:

Node Explanation
^ the beginning of the string anchor
( group and capture to \1:
\[ [
.*? any character except \n (0 or more times (matching the least amount possible))
\] ]
) end of \1
( group and capture to \2:
.* any character except \n (0 or more times (matching the most amount possible))
) end of \2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .