Let us first start by analyzing what went wrong before trying a solution. Here is your command:
sed -i 's/TRACK 02/TRACK 03/2' *cue
I wouldn't use the -i option of sed, but that is a secondary issue. sed
works on lines, not on files with text and a regular expression like "TRACK 02" describes the content of a line, not a file. What your command says is:
If a line contains the string "TRACK 02" twice (or more often), then change the second occurrence of it to "TRACK 03". Like this:
TRACK 02 TRACK 02 TRACK 02 TRACK 02 [...] ==>
TRACK 02 TRACK 03 TRACK 02 TRACK 02 [...]
Also notice, that regular expressions (or, rather, the underlying regular grammars are context-free by definition. Therefore there are some limitations on what you can do. For more information this is a starter, if you are interested I suggest the "Dragon book" ("Compilers: Principles, Techniques, and Tools" by Aho, Sethi and Ullman) for more in-depth analysis.
Another thing you need to understand is how sed works: It reads in the first line of the text to change, then applies the first command, on the result of this it applies the second command and so on, until the last command is reached. If you do not use the -n option then the result is printed, the next line is read in, to which the first command is applied, etc., until the last command is applied to the result of the penultimate command on the last line - upon which sed stops.
This is why you can't do this:
s/TRACK 00/TRACK 01/
s/TRACK 01/TRACK 02/
s/TRACK 02/TRACK 03/
s/TRACK 03/TRACK 04/
This would change "00" to "01", the next command would change "01" to "02", the next "02" to "03" and so on. In the end all lines would read "TRACK 04" (or however long you want to make the list).
What you can do, however, is to do the change in reverse (if you have more than 10 titles add similar lines at the beginning). While we are at it we can also add the pre-gap TRACK 01 automatically:
sed 's/TRACK 09/TRACK 10/
s/TRACK 08/TRACK 09/
s/TRACK 07/TRACK 08/
s/TRACK 06/TRACK 07/
s/TRACK 05/TRACK 06/
s/TRACK 04/TRACK 05/
s/TRACK 03/TRACK 04/
s/TRACK 02/TRACK 03/
s/TRACK 01/TRACK 02/
/FILE/ { a\
TRACK 01
a\
addtional optional text under TRACK 01
}' /path/to/file
Finally, here is a suggestion how to employ this sed-Script for mass-changings. We put the changed files in some other directory leaving the originals alone. If (only if) we have checked the results and are pleased with them we copy them back overwriting the originals:
for fWork in "*cue" ; do
sed 'the script from above' "$fWork" > "/other/path/$fWork"
done
Now, go check the results. If it is to your liking:
mv /other/path/*cur /original/path
s/pattern/replacement/n
then
refers to the nth occurrence of pattern within the current pattern space. By default, sed processes line-by-line so it's not going to do what you want here unless (for example) you slurp the whole file into pattern space using GNU sed's-z
option. – steeldriver Jul 16 '23 at 21:57{}
icon, or by adding a line containing three backticks before and after the text. – cas Jul 17 '23 at 05:33sed
? – jubilatious1 Jul 17 '23 at 09:41