Bash File Name Matching - Binary file .ogg matches !!!
Hello,
I am trying to loop through a directory of songs to rename them from a text file. This block is inside another loop which controls which entry from the file is currently being searched for. for i in $(ls *.ogg); do #if i contains the number 'N_' which matches '$i' then we make the swap. if grep "$NEWNAME" "$i"; then echo "MATCH Found" #Make rename here. fi #End of the Loop. done The problem is that $i is being treated as a binary file (obvious enough) so the if statment always returns true. Thanks. p.s. No I do NOT want to do this in Perl!!! :) How can I just check it name against the name of the variable $i and not the actual file itself... |
Code:
if [ `echo $i | grep $NEWNAME | wc -l` -ne 0 ] ; then Code:
if [ `echo $i | grep -c $NEWNAME` -ne 0 ] ; then |
Giving a sample entry from the file might help as well as a sample filename.
Your grep command looks backwords. Code:
for i in $(ls *.ogg); do Code:
if grep "$NEWNAME" "$i"; then If the original names are all like "1001.ogg" and the entries are like "1001_New_Name.ogg", then you could use something like "${song#[[:digit:]]*_}" to get just the "New_Name.ogg" part and "${song%_*.ogg}" to return 1001.ogg. So the rename command could be: mv "${song#[[:digit:]]*_}" "${song%_*.ogg}.ogg" |
Please post code in [code] tags to improve readability.
You don't need to execute ls to get a list of files - just use the pattern. This saves the invocation of an external program - this is something you should always strive to do. i.e. Code:
# Do this: If you want to test if the file name matches some pattern (e.g. starts with some digits followed by an underscore), consider using the case statement: Code:
#!/bin/bash |
Lots to look through here!
Ok, your right, I am basically trying to right a small script that will rename a directory of nameless ogg files with names provided from a text file. This is for when I have encoded stuff and do not have an internet connection and could not be bothered to type in all the names ... matthewg42, I am trying to go with your suggestion first of all. However I need to match $i against a value in another array (${anarray[$a]) as opposed to +([0-9])_*) . I tried this: Code:
for i in *.ogg; do Thankyou to everyone for your help though. Max. |
Using a string as an index to an array is an associative array (a map in C++ STL language, or a hash in Perl terminology). Sadly, bash doesn't really support associative arrays at the moment. I'm sure someone out there has a hackish way to do it, but for a nice clean and readable program, I'd recommend using a different shell, or even moving up a level of sophistication to a language like Perl or Python or awk. Awk is probably the smallest dependency, and it's installed by default on most unix-like OSes).
So here's some alternatives for associative arrays: 1. Use zsh. This is broadly compatible with bash scripts, so if you have a lot of script already written, it's probably the easiest option. Bare in mind it's not likely to be installed by default on most systems. Note that you must declare an associative array using th typedef -A statement first: Code:
#!/usr/bin/zsh Code:
#!/usr/bin/awk -f Code:
#!/usr/bin/perl |
I don't deny your statement about Perl being the best. I just wanted to try and learn more bash ...
Perhaps I should chill and just be happy with the existence of Perl ... However, just to say, with ${anarray[$a]}, the $a was only a number and I was not really treating it as associative. Are you quite certain I could not have any variable in the case statement? How about: Code:
case "$i" in Once again, thankyou. |
Oh I see, regular arrays with numeric indexes should be fine.
You can use a case statement with a variable, but you must use the correct syntax - you missed a ")" after your variable. Also, how are you setting the array? If you try to set an array item using a shell glob pattern, the pattern will be pre-expanded to match the pattern, so if you do this: Code:
ar=(*.ogg) |
Done!
Ok, at last. Well it seems to work.
I tested it with an album and it worked a treat. Thanks again for your help! I can now rename loads of untitled music files. ;) Max. Code:
#!/bin/bash |
All times are GMT -5. The time now is 11:32 AM. |