Rename file name between two points
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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--)/12/'
done
scripting apple
add a comment |Â
up vote
0
down vote
favorite
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--)/12/'
done
scripting apple
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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--)/12/'
done
scripting apple
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--)/12/'
done
scripting apple
edited Feb 9 at 8:55
ilkkachu
49.6k673137
49.6k673137
asked Feb 8 at 23:09
Chad
31
31
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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 .*
)
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
add a comment |Â
up vote
1
down vote
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 thefoo
regex and replace it withbar
. It will operate on all files matching the glob000*
.(.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 findingLabelMe--
.$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 includingLabelMe--
.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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 .*
)
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
add a comment |Â
up vote
0
down vote
accepted
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 .*
)
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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 .*
)
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 .*
)
answered Feb 9 at 9:20
ilkkachu
49.6k673137
49.6k673137
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
add a comment |Â
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
â Chad
Feb 9 at 14:51
add a comment |Â
up vote
1
down vote
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 thefoo
regex and replace it withbar
. It will operate on all files matching the glob000*
.(.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 findingLabelMe--
.$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 includingLabelMe--
.
add a comment |Â
up vote
1
down vote
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 thefoo
regex and replace it withbar
. It will operate on all files matching the glob000*
.(.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 findingLabelMe--
.$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 includingLabelMe--
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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 thefoo
regex and replace it withbar
. It will operate on all files matching the glob000*
.(.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 findingLabelMe--
.$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 includingLabelMe--
.
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 thefoo
regex and replace it withbar
. It will operate on all files matching the glob000*
.(.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 findingLabelMe--
.$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 includingLabelMe--
.
answered Feb 8 at 23:16
Sparhawk
8,35863487
8,35863487
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422927%2frename-file-name-between-two-points%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password