Results 1 to 6 of 6
Hello
input files:
Code:
01 - album - track001.mp3
09 - album - track009.mp3
desired output files:
Code:
track01.mp3
track09.mp3
works perfect:
Code:
ls *.mp3 | sed -ne 's/\([0-9]\{2\}\) - ...
- 08-02-2009 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 49
Script to rename multiple files
Hello
input files:
desired output files:Code:01 - album - track001.mp3 09 - album - track009.mp3
works perfect:Code:track01.mp3 track09.mp3
also works perfectCode:ls *.mp3 | sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp'
But when i want to "compose" both commands it gives me no outputCode:#!/bin/bash FS=$'\n' # Delimiter set to newline (do not include another whitespaces) for x in *.mp3 do echo $x done unset IFS
also my goal using mv in for loop is worthless if this simple echo did not work. What I am doing wrong ? I am Bash newbie trying to learn scripting, so if someone could do that in bash it would be nice. Many ThanksCode:#!/bin/bash FS=$'\n' for x in *.mp3 do echo "`sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x"`" done unset IFS
- 08-02-2009 #2
I am going to guess that the problem here has to do with quoting (dealing with multiple levels of quotes in Bash can be a pain). Fortunately, you can simplify your script. Simply change it to this:
There is no need to echo "`command`". A line that is unquoted is simply executed (for instance, in your original script, the "echo" is executed: this is unnecessary.Code:#!/bin/bash FS=$'\n' for x in *.mp3 do sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x" done unset IFS
Having said that, I don't understand why you are using this final script in any case. The first solution that you posted (piping the output of ls into sed) is the more efficient way to do this, and is certainly the easiest. If it's just a learning experience, then all is good, but if you're looking for a final solution, go with the first one
.
Hope this helps.DISTRO=Arch
Registered Linux User #388732
- 08-02-2009 #3Just Joined!
- Join Date
- Jul 2009
- Posts
- 49
Hello I use echo for "debugging purposes". If i omit echo i definetly lost output of script (Or am i wrong ?).
I treid also followings for viewing output (but no result):
Simply when i run the script i get jus blank output consist of (number of *.mp3) newlines.Code:sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x" | echo sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x" >> output
what i am trying to do is simple copy files: from "01 - album - track001.mp3" format to "track01.mp3" format
gives following output:Code:#!/bin/bash FS=$'\n' for x in *.mp3 do cp "$x" "`sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x"`" done unset IFS
I think this task can't be done just using simple piping, if i am wrong correct me.Code:mv: cannot move `01 - album - track001.mp3' to `': No such file or directory mv: cannot move `02 - album - track002.mp3' to `': No such file or directory mv: cannot move `03 - album - track003.mp3' to `': No such file or directory mv: cannot move `04 - album - track004.mp3' to `': No such file or directory mv: cannot move `05 - album - track005.mp3' to `': No such file or directory mv: cannot move `06 - album - track006.mp3' to `': No such file or directory mv: cannot move `07 - album - track007.mp3' to `': No such file or directory mv: cannot move `08 - album - track008.mp3' to `': No such file or directory mv: cannot move `09 - album - track009.mp3' to `': No such file or directory
Thank you
- 08-02-2009 #4Just Joined!
- Join Date
- Jul 2009
- Posts
- 58
I think you're better off doing the rename, and the 00 -> 0 replacement in two steps, it's just making it harder for no extra gain.
echo $foo | awk '{print $5}' should split on space and give you the .mp3 filename.
After you do the rename, you can go back and find and replace 00 with 0, with sed 's/00/0/'.
- 08-02-2009 #5Just Joined!
- Join Date
- Jul 2009
- Posts
- 49
OK I made it!
but is stil strange of me whyCode:#!/bin/bash FS=$'\n' for x in *.mp3 do var="`echo -ne $x | sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' `" cp "$x" "$var" done unset IFS
Code:var="`echo -ne $x | sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' is not the same thing like i did before var="`sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' "$x"`"
- 10-17-2009 #6Just Joined!
- Join Date
- Jul 2009
- Posts
- 49


Reply With Quote
