0

I am looking for a way to rename a batch of videos by remove text between two points. Specifically I would like to remove everything after the 6th character, to the last "LabelMe--".

0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov

0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov

0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov

I am trying to do this using Apple's Automator Services. Right-click on a bunch of files and select the service to clean up the name. I found one piece of code but am not sure I am using it correctly.

for f in "$@"
do 
    echo sed -e 's/\(--LabelMe).*\(LabelMe--)/\1\2/'
done

enter image description here

ilkkachu
  • 138,973
Chad
  • 3

2 Answers2

1

Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.

Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.

$ rename 's/(.{6}).*LabelMe--/$1/' 000* -n
0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov

Once you are happy with the result, run it for real, i.e. rename 's/(.{6}).*LabelMe--/$1/' 000*

Explanation

  • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.
  • (.{6}).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .{6}, which you put into a capturing group (.{6}). After this, you can have any characters .*, before finding LabelMe--.
  • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.{6}) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.
Sparhawk
  • 19,941
0

There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)

You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:

mv "$f" "$(echo "$f" | sed -e '...' )"

The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the \N references:

's/--LabelMe.*LabelMe--//'

So in full:

for f in "$@"
do 
    mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
done

Another way would be to use the shell's parameter substitution to do the same. The substitution ${par/pattern/replacement} is nonstandard, but supported in many shells:

for f in "$@"
do 
    mv "$f" "${f/--LabelMe*LabelMe--/}"
done

(note that's not a regex, so the syntax for "anything" is just *, and not .*)

ilkkachu
  • 138,973
  • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much. – Chad Feb 09 '18 at 14:51