Bash: minus one to the sub string split by separator2019 Community Moderator ElectionRename multiple directories decrementing sequence number?Renaming Files according to PatternBatch file rename with substitution lookuprename a list of files according to a text fileParsing string on multiple parts without separatorhow can I target filenames with 2 digits with the rename tool?Find and rename files (including directories) whose filenames contain spacePad number within string and rename fileunderstand the order of operations for bash parameter expansionCount the number of occurrences of a substring in a string
The IT department bottlenecks progress. How should I handle this?
What is Cash Advance APR?
Redundant comparison & "if" before assignment
Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed
Problem with TransformedDistribution
Creepy dinosaur pc game identification
Fear of getting stuck on one programming language / technology that is not used in my country
How do you make your own symbol when Detexify fails?
WiFi Thermostat, No C Terminal on Furnace
How to indicate a cut out for a product window
The screen of my macbook suddenly broken down how can I do to recover
When were female captains banned from Starfleet?
Where does the bonus feat in the cleric starting package come from?
why `nmap 192.168.1.97` returns less services than `nmap 127.0.0.1`?
Did arcade monitors have same pixel aspect ratio as TV sets?
Loading commands from file
How should I respond when I lied about my education and the company finds out through background check?
Closed-form expression for certain product
250 Floor Tower
C++ debug/print custom type with GDB : the case of nlohmann json library
Should I outline or discovery write my stories?
Why is so much work done on numerical verification of the Riemann Hypothesis?
Is the U.S. Code copyrighted by the Government?
Has any country ever had 2 former presidents in jail simultaneously?
Bash: minus one to the sub string split by separator
2019 Community Moderator ElectionRename multiple directories decrementing sequence number?Renaming Files according to PatternBatch file rename with substitution lookuprename a list of files according to a text fileParsing string on multiple parts without separatorhow can I target filenames with 2 digits with the rename tool?Find and rename files (including directories) whose filenames contain spacePad number within string and rename fileunderstand the order of operations for bash parameter expansionCount the number of occurrences of a substring in a string
I would like to batch rename filenames turningA-B-C#2-D.wav
to A-B-C#1-D.wav
. So for example:
A-B-C#2-D.wav
A-B-C#8-G.wav
A-B-C6-E.wav
becomes
A-B-C#1-D.wav
A-B-C#7-G.wav
A-B-C5-E.wav
So the number in the third substring should be decreased by one.
(#
is the part of filenames instead of comment; both filenames with and without #
are possible.)
bash shell-script rename mv
New contributor
add a comment |
I would like to batch rename filenames turningA-B-C#2-D.wav
to A-B-C#1-D.wav
. So for example:
A-B-C#2-D.wav
A-B-C#8-G.wav
A-B-C6-E.wav
becomes
A-B-C#1-D.wav
A-B-C#7-G.wav
A-B-C5-E.wav
So the number in the third substring should be decreased by one.
(#
is the part of filenames instead of comment; both filenames with and without #
are possible.)
bash shell-script rename mv
New contributor
Is the third example actually...C#6?
– Jeff Schaller
yesterday
1
No, sorry for the misleading. It is what it is to highlight they can be both with and without#
– Francis
yesterday
Not a duplicate, butvim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1
– pfnuesel
yesterday
Please describe exactly the format of the file names. Is there always exactlyA-B-C
or shouldA
,B
,C
stand for any character (except-
) or for a string of variable length (without-
)? Does the file name always contain exactly 3-
? Does the number always precede immediately the 3rd-
? Do the numbers always consist of exactly 1 digit? If number0
is possible, should it be replaced with-1
? ...
– Bodo
yesterday
add a comment |
I would like to batch rename filenames turningA-B-C#2-D.wav
to A-B-C#1-D.wav
. So for example:
A-B-C#2-D.wav
A-B-C#8-G.wav
A-B-C6-E.wav
becomes
A-B-C#1-D.wav
A-B-C#7-G.wav
A-B-C5-E.wav
So the number in the third substring should be decreased by one.
(#
is the part of filenames instead of comment; both filenames with and without #
are possible.)
bash shell-script rename mv
New contributor
I would like to batch rename filenames turningA-B-C#2-D.wav
to A-B-C#1-D.wav
. So for example:
A-B-C#2-D.wav
A-B-C#8-G.wav
A-B-C6-E.wav
becomes
A-B-C#1-D.wav
A-B-C#7-G.wav
A-B-C5-E.wav
So the number in the third substring should be decreased by one.
(#
is the part of filenames instead of comment; both filenames with and without #
are possible.)
bash shell-script rename mv
bash shell-script rename mv
New contributor
New contributor
edited yesterday
ilkkachu
62.4k10103179
62.4k10103179
New contributor
asked yesterday
FrancisFrancis
1011
1011
New contributor
New contributor
Is the third example actually...C#6?
– Jeff Schaller
yesterday
1
No, sorry for the misleading. It is what it is to highlight they can be both with and without#
– Francis
yesterday
Not a duplicate, butvim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1
– pfnuesel
yesterday
Please describe exactly the format of the file names. Is there always exactlyA-B-C
or shouldA
,B
,C
stand for any character (except-
) or for a string of variable length (without-
)? Does the file name always contain exactly 3-
? Does the number always precede immediately the 3rd-
? Do the numbers always consist of exactly 1 digit? If number0
is possible, should it be replaced with-1
? ...
– Bodo
yesterday
add a comment |
Is the third example actually...C#6?
– Jeff Schaller
yesterday
1
No, sorry for the misleading. It is what it is to highlight they can be both with and without#
– Francis
yesterday
Not a duplicate, butvim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1
– pfnuesel
yesterday
Please describe exactly the format of the file names. Is there always exactlyA-B-C
or shouldA
,B
,C
stand for any character (except-
) or for a string of variable length (without-
)? Does the file name always contain exactly 3-
? Does the number always precede immediately the 3rd-
? Do the numbers always consist of exactly 1 digit? If number0
is possible, should it be replaced with-1
? ...
– Bodo
yesterday
Is the third example actually...C#6?
– Jeff Schaller
yesterday
Is the third example actually...C#6?
– Jeff Schaller
yesterday
1
1
No, sorry for the misleading. It is what it is to highlight they can be both with and without
#
– Francis
yesterday
No, sorry for the misleading. It is what it is to highlight they can be both with and without
#
– Francis
yesterday
Not a duplicate, but
vim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1– pfnuesel
yesterday
Not a duplicate, but
vim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1– pfnuesel
yesterday
Please describe exactly the format of the file names. Is there always exactly
A-B-C
or should A
, B
, C
stand for any character (except -
) or for a string of variable length (without -
)? Does the file name always contain exactly 3 -
? Does the number always precede immediately the 3rd -
? Do the numbers always consist of exactly 1 digit? If number 0
is possible, should it be replaced with -1
? ...– Bodo
yesterday
Please describe exactly the format of the file names. Is there always exactly
A-B-C
or should A
, B
, C
stand for any character (except -
) or for a string of variable length (without -
)? Does the file name always contain exactly 3 -
? Does the number always precede immediately the 3rd -
? Do the numbers always consist of exactly 1 digit? If number 0
is possible, should it be replaced with -1
? ...– Bodo
yesterday
add a comment |
2 Answers
2
active
oldest
votes
With zsh
:
autoload zmv # best in ~/.zshrc
zmv -n '(*[^0-9])(<1->)(*.wav)(#qn)' '$1$(($2-1))$3'
(remove the -n
if satisfied)
The (#qn)
is for the list to be numerically sorted so that A-B-C#9-D.wav
be renamed before A-B-C#10-D.wav
for instance.
However, if there were both a A-B-C#9-D.wav
and A-B-C#10-D.wav
, zmv
would flag the fact that one file would be renamed to the name of an existing file and abort the command. You'd need to add the -f
option to still force it.
Or with zsh (for the numeric glob order) and perl
's rename:
rename -n 's/d+/$&-1/e' ./*[1-9]*.wav(#qn)
(same caveat and same -f
option with some variants of rename
).
add a comment |
With a bash-specific loop (for the regular-expression testing conditional =~
):
for file in ?-?-*-?.wav
do
[[ $file =~ ^([^[:digit:]]+)([[:digit:]]+)(-..wav)$ ]] &&
echo mv -- "$file" "$BASH_REMATCH[1]$((10#$BASH_REMATCH[2] - 1))$BASH_REMATCH[3]"
done
This uses a wildcard to pick up the desired wav
files; matching files will have one character (letter) between the dashes and before the .wav
at the end. Each filename is run through the regular expression, which separates it into three pieces:
^([^[:digit:]]+)
-- the leading portion; everything up to the first digit([[:digit:]]+)
-- the digit(s)(-..wav)
-- the trailing portion
If the match succeeds, then we rename the file, using the leading portion, the digits minus one, and the trailing portion.
Remove the echo
when the output looks correct.
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Francis is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508016%2fbash-minus-one-to-the-sub-string-split-by-separator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With zsh
:
autoload zmv # best in ~/.zshrc
zmv -n '(*[^0-9])(<1->)(*.wav)(#qn)' '$1$(($2-1))$3'
(remove the -n
if satisfied)
The (#qn)
is for the list to be numerically sorted so that A-B-C#9-D.wav
be renamed before A-B-C#10-D.wav
for instance.
However, if there were both a A-B-C#9-D.wav
and A-B-C#10-D.wav
, zmv
would flag the fact that one file would be renamed to the name of an existing file and abort the command. You'd need to add the -f
option to still force it.
Or with zsh (for the numeric glob order) and perl
's rename:
rename -n 's/d+/$&-1/e' ./*[1-9]*.wav(#qn)
(same caveat and same -f
option with some variants of rename
).
add a comment |
With zsh
:
autoload zmv # best in ~/.zshrc
zmv -n '(*[^0-9])(<1->)(*.wav)(#qn)' '$1$(($2-1))$3'
(remove the -n
if satisfied)
The (#qn)
is for the list to be numerically sorted so that A-B-C#9-D.wav
be renamed before A-B-C#10-D.wav
for instance.
However, if there were both a A-B-C#9-D.wav
and A-B-C#10-D.wav
, zmv
would flag the fact that one file would be renamed to the name of an existing file and abort the command. You'd need to add the -f
option to still force it.
Or with zsh (for the numeric glob order) and perl
's rename:
rename -n 's/d+/$&-1/e' ./*[1-9]*.wav(#qn)
(same caveat and same -f
option with some variants of rename
).
add a comment |
With zsh
:
autoload zmv # best in ~/.zshrc
zmv -n '(*[^0-9])(<1->)(*.wav)(#qn)' '$1$(($2-1))$3'
(remove the -n
if satisfied)
The (#qn)
is for the list to be numerically sorted so that A-B-C#9-D.wav
be renamed before A-B-C#10-D.wav
for instance.
However, if there were both a A-B-C#9-D.wav
and A-B-C#10-D.wav
, zmv
would flag the fact that one file would be renamed to the name of an existing file and abort the command. You'd need to add the -f
option to still force it.
Or with zsh (for the numeric glob order) and perl
's rename:
rename -n 's/d+/$&-1/e' ./*[1-9]*.wav(#qn)
(same caveat and same -f
option with some variants of rename
).
With zsh
:
autoload zmv # best in ~/.zshrc
zmv -n '(*[^0-9])(<1->)(*.wav)(#qn)' '$1$(($2-1))$3'
(remove the -n
if satisfied)
The (#qn)
is for the list to be numerically sorted so that A-B-C#9-D.wav
be renamed before A-B-C#10-D.wav
for instance.
However, if there were both a A-B-C#9-D.wav
and A-B-C#10-D.wav
, zmv
would flag the fact that one file would be renamed to the name of an existing file and abort the command. You'd need to add the -f
option to still force it.
Or with zsh (for the numeric glob order) and perl
's rename:
rename -n 's/d+/$&-1/e' ./*[1-9]*.wav(#qn)
(same caveat and same -f
option with some variants of rename
).
edited yesterday
answered yesterday
Stéphane ChazelasStéphane Chazelas
311k57586945
311k57586945
add a comment |
add a comment |
With a bash-specific loop (for the regular-expression testing conditional =~
):
for file in ?-?-*-?.wav
do
[[ $file =~ ^([^[:digit:]]+)([[:digit:]]+)(-..wav)$ ]] &&
echo mv -- "$file" "$BASH_REMATCH[1]$((10#$BASH_REMATCH[2] - 1))$BASH_REMATCH[3]"
done
This uses a wildcard to pick up the desired wav
files; matching files will have one character (letter) between the dashes and before the .wav
at the end. Each filename is run through the regular expression, which separates it into three pieces:
^([^[:digit:]]+)
-- the leading portion; everything up to the first digit([[:digit:]]+)
-- the digit(s)(-..wav)
-- the trailing portion
If the match succeeds, then we rename the file, using the leading portion, the digits minus one, and the trailing portion.
Remove the echo
when the output looks correct.
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
add a comment |
With a bash-specific loop (for the regular-expression testing conditional =~
):
for file in ?-?-*-?.wav
do
[[ $file =~ ^([^[:digit:]]+)([[:digit:]]+)(-..wav)$ ]] &&
echo mv -- "$file" "$BASH_REMATCH[1]$((10#$BASH_REMATCH[2] - 1))$BASH_REMATCH[3]"
done
This uses a wildcard to pick up the desired wav
files; matching files will have one character (letter) between the dashes and before the .wav
at the end. Each filename is run through the regular expression, which separates it into three pieces:
^([^[:digit:]]+)
-- the leading portion; everything up to the first digit([[:digit:]]+)
-- the digit(s)(-..wav)
-- the trailing portion
If the match succeeds, then we rename the file, using the leading portion, the digits minus one, and the trailing portion.
Remove the echo
when the output looks correct.
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
add a comment |
With a bash-specific loop (for the regular-expression testing conditional =~
):
for file in ?-?-*-?.wav
do
[[ $file =~ ^([^[:digit:]]+)([[:digit:]]+)(-..wav)$ ]] &&
echo mv -- "$file" "$BASH_REMATCH[1]$((10#$BASH_REMATCH[2] - 1))$BASH_REMATCH[3]"
done
This uses a wildcard to pick up the desired wav
files; matching files will have one character (letter) between the dashes and before the .wav
at the end. Each filename is run through the regular expression, which separates it into three pieces:
^([^[:digit:]]+)
-- the leading portion; everything up to the first digit([[:digit:]]+)
-- the digit(s)(-..wav)
-- the trailing portion
If the match succeeds, then we rename the file, using the leading portion, the digits minus one, and the trailing portion.
Remove the echo
when the output looks correct.
With a bash-specific loop (for the regular-expression testing conditional =~
):
for file in ?-?-*-?.wav
do
[[ $file =~ ^([^[:digit:]]+)([[:digit:]]+)(-..wav)$ ]] &&
echo mv -- "$file" "$BASH_REMATCH[1]$((10#$BASH_REMATCH[2] - 1))$BASH_REMATCH[3]"
done
This uses a wildcard to pick up the desired wav
files; matching files will have one character (letter) between the dashes and before the .wav
at the end. Each filename is run through the regular expression, which separates it into three pieces:
^([^[:digit:]]+)
-- the leading portion; everything up to the first digit([[:digit:]]+)
-- the digit(s)(-..wav)
-- the trailing portion
If the match succeeds, then we rename the file, using the leading portion, the digits minus one, and the trailing portion.
Remove the echo
when the output looks correct.
edited yesterday
answered yesterday
Jeff SchallerJeff Schaller
43.8k1161141
43.8k1161141
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
add a comment |
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
Good catch; thank you Stéphane! I've tweaked the regex to be less greedy and also forced base-10 in the arithmetic.
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
well spotted again; I've simplified the regex to mirror yours and gobble up non-digits ahead of the digits
– Jeff Schaller
yesterday
add a comment |
Francis is a new contributor. Be nice, and check out our Code of Conduct.
Francis is a new contributor. Be nice, and check out our Code of Conduct.
Francis is a new contributor. Be nice, and check out our Code of Conduct.
Francis is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508016%2fbash-minus-one-to-the-sub-string-split-by-separator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is the third example actually...C#6?
– Jeff Schaller
yesterday
1
No, sorry for the misleading. It is what it is to highlight they can be both with and without
#
– Francis
yesterday
Not a duplicate, but
vim
can do this relatively easy. See e.g. stackoverflow.com/questions/19195503/vim-replace-n-with-n1– pfnuesel
yesterday
Please describe exactly the format of the file names. Is there always exactly
A-B-C
or shouldA
,B
,C
stand for any character (except-
) or for a string of variable length (without-
)? Does the file name always contain exactly 3-
? Does the number always precede immediately the 3rd-
? Do the numbers always consist of exactly 1 digit? If number0
is possible, should it be replaced with-1
? ...– Bodo
yesterday