Find the answer to your Linux question:
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\}\) - ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    49

    Question Script to rename multiple files

    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\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp'
    also works perfect
    Code:
    #!/bin/bash
    FS=$'\n'	# Delimiter set to newline (do not include another whitespaces)
    for x  in *.mp3
    do
     echo $x
    done
    unset IFS
    But when i want to "compose" both commands it gives me no output
    Code:
    #!/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
    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 Thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    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
    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.

    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

  3. #3
    Just 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):
    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
    Simply when i run the script i get jus blank output consist of (number of *.mp3) newlines.

    what i am trying to do is simple copy files: from "01 - album - track001.mp3" format to "track01.mp3" format
    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
    gives following output:

    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
    I think this task can't be done just using simple piping, if i am wrong correct me.
    Thank you

  4. #4
    Just 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/'.

  5. #5
    Just Joined!
    Join Date
    Jul 2009
    Posts
    49
    OK I made it!

    Code:
    #!/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
    but is stil strange of me why
    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"`"

  6. #6
    Just Joined!
    Join Date
    Jul 2009
    Posts
    49
    Quote Originally Posted by wakatana View Post
    OK I made it!

    but is stil strange of me why
    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"`"
    now i know this bot arent the same....
    first one print the string with echo, then does sed on this string and finaly stores it to variable.
    the second do the sed on the contents of the file and stores it to variable... and this is mistake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...