I am not sure I follow the original code logic?? Apart from the fact I do not see why there are 2 for loops, surely one was enough, but the seds in both loops would seem to be flawed to me, based on what was written as the original brief?
Quote:
I wanted to take the spaces out to prevent problems with the script, replace the hyphens with underscores and then run them through an mkv2avi script I took off the internet for conversions.
|
If we use the example given -: Bleach - 137 -.mkv
The first for loop sed -:
Code:
sed 's/\(.[-]\)./\_/g;s/_mkv/.mkv/g'
Firstly, the use of a back reference and escaping the underscore, none of these are required.
Now the output of this looks nice, but it does not meet the brief as we are to remove spaces and replace hyphens with underscores, however, this removes any character, followed by a hyphen, followed by any
other character and replaces the lot with an underscore. Yes you know the format of the original string so using any character instead of a space either side of a hyphen makes a little sense, but,
being that you now have to add an additional sed change to allow for the fact that you turned a period into an underscore (not in the brief), I consider this lazy coding.
So the current output is:
Spaces removed (check), hyphens replaced with underscores (wrong), we started with 2 hyphens and finished with 1 underscore. Again, format wise I understand, but based on the brief, this is wrong.
So based on the known format and the brief, the following would produce what was asked for:
Code:
sed -r 's/[ -]+/_/g'
This would produce:
Whilst not the 'desired' outcome, it is what was asked for.
The second for loop sed:
Code:
sed 's/\([0-9]_\)\([0-9]_\)\(.mkv\)/\1\2\.avi/g'
We can look at the affect on both the current and the requested output:
1. Current (Bleach_137.mkv) -: changes - - nothing
2. Requested (Bleach_137_.mkv) -: changes - - nothing
Reasons for no changes in either, the sed is looking for a digit followed by an underscore followed by a digit and another underscore. As we can see from both examples, this pattern appears nowhere. Cannot really assist with this one as apart from wanting to change 'mkv' to 'avi' I am not sure what the other change requirement is?
I will say that for sed, if you use -r it saves the need to escape all of your round brackets for back references and that the saving of '.mkv', which is any character followed by 'mkv', is
also not required here.
Lastly, as pointed out by unSpawn, if you wish to improve your bash skills, then sed is not required as parameter substitution can perform the changes you are looking for (caveat: except the last sed as
at this stage we are not aware of its requirement).
Hope some of that helps