0
  • perl-rename 1.14-1

  • Arch Linux: linux 6.2.12.arch1-1

I have a directory/folder with several subdirectories, all are named by one of the following patterns

Status quo of existing patterns

something-AAA-2023-01-BBB (the most common pattern) something-2023-01-AAA.BBB (the 2nd or 3rd most common pattern) 2023-01-AAA-BBB-CCC.DDD (the 2nd or 3rd most common pattern) something-AAA-2023-01-BBB-CCC.DDD 1111-AAA-2023-01-BBB.CCC 1111-2023-01-AAA.BBB 

About these patterns:

  • The "something" always is a string, the "1111" a number.
  • One part is a date, formatted by YYYY-MM, represented by "2023-01" for the sake of clarity.
  • The parts of 3 capital letters AAA, BBB, etc. sometimes are either normal words which make sense in the context of the whole folder or sometimes are a jumbled combination of letters and numbers which also make sense in the context of the whole thing. All these AAA's, BBB's, etc. are expressions which make sense in the context of the folder.
  • Over time, I added additional information into the folder names, which is why there are CCC's and DDD's.

Expected output

Pending their occurence, I would like to have the elements ("1111"., AAA's, BBB's, etc.) be put out like

1111.AAA.2023-01.something.BBB.CCC.DDD 

I would like to start off by finding/catching the content between the 1st and 2nd hyphen (if it is there).

So far I am at

perl-rename -n 's/^(.+)\-(.+)\-(\d{4}\-\d{2})\-(.+)/\3\.\1\./' * 

but that is not really "smart". Is there a way to check if the date is between the 1st and 2nd hyhpen or 2nd and 3rd, before the date (\d{4}\-\d{2}) or after? Or rather, do I have to find all the different cases by defining the number of parentheses of content before and after the date? (sorry for my English there)

Or how can I check if the hyphen before that date string is the 2nd hyphen? Finding the nth occurrence of a character will certainly help in the future.

6
  • 1
    What is the expected output? Please add it to your post, no comment Commented Apr 22, 2023 at 16:49
  • 1
    Why is that "not really "smart""? Does it do what you want? If so, what's wrong? If not, then what do you want to do instead? Commented Apr 22, 2023 at 17:16
  • @GillesQuénot Thank your for your comment and reply. I edited the post in a general fashion to create a hopefully more concrete and complete picture of the situation. Commented Apr 22, 2023 at 22:31
  • There's only one expected output file while there's 3 treated from your sample rename command Commented Apr 22, 2023 at 22:56
  • And there's no input files that contains 1111, AAA, BBB, something Commented Apr 22, 2023 at 23:12

1 Answer 1

0

You could try something like this, using autosplit mode with -F to split columns based on - separator, than can facilitate manipulation.

To discover position of n-th element with the date string, of @F auto generated array, here the code:

$ ls 1111-AAA-2023-01-BBB.CCC 
$ perl -F'-' -nE ' foreach my $i (keys @F) { say $i if "$F[$i]-$F[$i+1]" =~ m/\d{4}-\d{2}/; } ' < <(printf '%s\n' *) 
2 
 1111-AAA-2023-01-BBB.CCC # ^^^^ ^^^ ^^^^ ^^ ^^^^^^^ # 0 1 2 3 4 keys of @F array 

2 is the key of the array element containing year. The next element is month.

Now, based on this, if you give me/us the input files that need to be renamed and the expected output, it will be easy to adapt to your needs with Perl command or even with code injected in rename.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.