0

I want to renaming several files with names, such as:

YL.BIRA..BHE.M.2001.324.210818.SAC

I want to rename it as:

YL.BIRA..M.2001.324.210818.BHE.SAC

I would appreciate your help. Thank you!

userene
  • 276

2 Answers2

2

Treating all those dot-separated strings as fields, you could obtain that result by moving the 4th field to the (after having removed the 4th field) 8th position (pushing the 9th field to the right);

Using rename you may do:

rename -n '
    my @f = split(/\./);
    splice(@f, 7, 0, splice(@f, 3, 1));
    $_ = join(".", @f);
' *

Which will show what the result of the transformation would be if applied to all the elements in the current working directory:

% rename -n '
    my @f = split(/\./);
    splice(@f, 7, 0, splice(@f, 3, 1));
    $_ = join(".", @f);
' *
rename(YL.BIRA..BHE.M.2001.324.210818.SAC, YL.BIRA..M.2001.324.210818.BHE.SAC)

To actually apply the transformation, remove the -n switch:

rename '
    my @f = split(/\./);
    splice(@f, 7, 0, splice(@f, 3, 1));
    $_ = join(".", @f);
' *
kos
  • 41,268
1

Although you tagged your question bash, if you are willing to switch to zsh you could use the contributed zmv module as follows (remove the -n after testing):

 % autoload -Uz zmv    # usually this would go in your ~/.zshrc

then

 % zmv -n -w '*.*..*.*.*.*.*.*' '$1.$2..$4.$5.$6.$7.$3.$8'
mv -- YL.BIRA..BHE.M.2001.324.210818.SAC YL.BIRA..M.2001.324.210818.BHE.SAC

or slightly more compactly

 % zmv -n '(*.*)..(*).(*.*.*.*).(*)' '$1..$3.$2.$4' 
mv -- YL.BIRA..BHE.M.2001.324.210818.SAC YL.BIRA..M.2001.324.210818.BHE.SAC

The first of these would also work in mmv (available from the Ubuntu "universe" repository) with minor changes:

$ mmv -n '*.*..*.*.*.*.*.*' '#1.#2..#4.#5.#6.#7.#3.#8'
YL.BIRA..BHE.M.2001.324.210818.SAC -> YL.BIRA..M.2001.324.210818.BHE.SAC
kos
  • 41,268
steeldriver
  • 142,475