Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
07-15-2009, 03:15 PM
|
#16
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by Qu3ry
1.new year.mp3
2.linux.mp3
3.hello.mp3
etc...
|
Sorry, I'm still not clear exactly what you want to do here. You want to rename which *.mp3 files? The "1-01 track.mp3" etc ones as "new year 01 track.mp3" etc?
|
|
|
|
07-15-2009, 03:53 PM
|
#17
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
Sorry, I'm still not clear exactly what you want to do here. You want to rename which *.mp3 files? The "1-01 track.mp3" etc ones as "new year 01 track.mp3" etc?
|
1-01 track.mp3 is one of the split files of 1.new year.mp3.
first to merge, then to rename them.
|
|
|
|
07-15-2009, 04:25 PM
|
#18
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by Qu3ry
1-01 track.mp3 is one of the split files of 1.new year.mp3.
first to merge, then to rename them.
|
How do you merge them?
|
|
|
|
07-16-2009, 04:02 PM
|
#19
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
cat 01-*.mp3 > 01.mp3
cat 02-*.mp3 > 02.mp3
etc..
Writing a for loop to automate the task. The problem is that you have to know number of tracks to be merged in each directory.
Code:
i = 01
j = 32 # number of tracks to be merged
for i <= j ; do
cat "$i"-*.mp3 > "$i".mp3;
i = $(i + 1)
done
|
|
|
|
07-16-2009, 08:50 PM
|
#20
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
What language are those for loops in? I'm pretty that is not valid bash.
This should work, assuming the song number is always separated by a "-" and is the first thing in the file name. Also it might screw up if there are other files in the directory.
Code:
for song in $(ls *-*.mp3 | cut -d- -f1 | sort -u) ; do
cat "$song"-*.mp3 > "$song".mp3
done
|
|
|
|
07-17-2009, 01:55 PM
|
#21
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
ntubski, your script should work. However, I found a new pattern in other directories.. Some songs aren't grouped together by their number prefixes. More precisely they are grouped by their names.. for instance..
01-1.song.mp3
01-2.song.mp3
01-3.pop.mp3
01-4.pop.mp3
There are two songs in those 4 files. How to change the script accordingly?
Last edited by Qu3ry; 07-17-2009 at 01:58 PM.
|
|
|
|
07-17-2009, 03:23 PM
|
#22
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Hello Qu3ry
Quote:
Originally Posted by Qu3ry
cat 01-*.mp3 > 01.mp3
Code:
i = 01
j = 32 # number of tracks to be merged
for i <= j ; do
cat "$i"-*.mp3 > "$i".mp3;
i = $(i + 1)
done
|
Bash requires no spaces either side of the = command in an assignment. Thus i = 01 does not assign 01 to i. Instead it tries to run the command i with operands = and 01.
Bash interprets integer constants beginning with 0 as octal. Doesn't make any difference for 01 to 07 but 09 would be invalid.
The syntax of the "for" statement is either
Code:
for name [in words ...]
or
Code:
for (( expr1 ; expr2 ; expr3 ))
It looks as if you wanted
Code:
while test-commands
The <= comparison operator is for strings unless you are doing shell arithmetic in a let <arithmetic expression>, ((<arithmetic expression>)) or $((<arithmetic expression>)). The bash test numeric comparison operator is -le.
In cat "$i"-*.mp3 > "$i".mp3; the ";" is OK but not necessary because the line end serves the same purpose.
i = $(i + 1) will not do what you want, first because of the spaces either side of the "=" and second because $(i + 1) will try to run command i with operands + and 1. i=((i + 1)) would do what you want because bash evaluates expressions within (( )) as arithmetic and substitutes their value. Assuming $i is 5 then i=((i + 1)) becomes i=6
This works:
Code:
for (( i=1; i<=32; i++ ))
do
cat "$i"-*.mp3 > "$i".mp3
done
BTW, I'm amazed and educated to learn that you can cat *.mp3 files together and play the result. Please confirm you have tested this.
Best
Charles
|
|
|
|
07-17-2009, 03:47 PM
|
#23
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
Quote:
Originally Posted by Qu3ry
01-1.song.mp3
01-2.song.mp3
01-3.pop.mp3
01-4.pop.mp3
There are two songs in those 4 files. How to change the script accordingly?
|
changes in red.
Code:
for song in $(ls *-*.*.mp3 | cut -d. -f2 | sort -u) ; do
cat *."$song".mp3 > "$song".mp3
done
Quote:
|
Originally Posted by catkin
BTW, I'm amazed and educated to learn that you can cat *.mp3 files together and play the result.
|
You know, now that you mention it, it does seem surprising.
Last edited by ntubski; 07-17-2009 at 03:49 PM.
Reason: more precise change indication
|
|
|
|
07-17-2009, 04:15 PM
|
#24
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
I have been using the cat command to merge avi and mp3 files for awhile with no problem.
For further reference: an article on looping filenames with spaces:
http://www.cyberciti.biz/tips/handli...s-in-bash.html
Last edited by Qu3ry; 07-17-2009 at 04:25 PM.
|
|
|
|
07-17-2009, 04:51 PM
|
#25
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Hello Qu3ry 
Quote:
Originally Posted by Qu3ry
|
Thanks for the info.
Slightly scary page on the link though ...
" By default $IFS is set to the space character" is not correct. By default it is set to space, tab and newline.
There's no need to adjust IFS for this code to work when files in the current directory contain spaces (and tabs and newlines!)
Code:
for f in *
do
echo "$f"
done
It works just fine with the default IFS!
I'm surprised that IFS=$SAVEIFS works. What if there are current IFS characters in $SAVEIFS? Wouldn't the shell evaluate $SAVEIFS into more than one word? IFS="$SAVEIFS" is safer. Mmm ...
In IFS=$(echo -en "\n\b"), why put the backspace character in IFS? Mmm ...
while IFS=: read userName passWord userID groupID geCos homeDir userShell is perfectly correct but neither transparent nor efficient; its only merit is to save a line. Functionally this is identical
Code:
IFS=:
while read userName passWord userID groupID geCos homeDir userShell
Best
Charles
|
|
|
|
07-18-2009, 07:54 AM
|
#26
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
Quote:
Originally Posted by ntubski
changes in red.
Code:
for song in $(ls *-*.*.mp3 | cut -d. -f2 | sort -u) ; do
cat *."$song".mp3 > "$song".mp3
done
You know, now that you mention it, it does seem surprising.
|
ntubski, the new script didn't work. There is no mp3 matches the pattern to begin with. I have changed the code to (ls *-*.mp3) to match the pattern and ran the code with the echo command instead of the cat command. It didn't work neither. Here is the result:
*.mp3.mp3 to mp3.mp3
where "to" is the replacement of ">"
Last edited by Qu3ry; 07-18-2009 at 10:15 AM.
|
|
|
|
07-18-2009, 08:24 AM
|
#27
|
|
LQ Newbie
Registered: Jul 2009
Distribution: ubuntu, fedora, OpenSolaris
Posts: 21
Original Poster
Rep:
|
catkin, I find using files=($(ls *.mp3)) as an array assignment not able to deal with filenames with spaces. Changing it to files=( *.mp3 ) can solve the problem.
Code:
files=( *.mp3 )
i=0
cat test.txt | while read j
do
mv "${files[ $i ]}" "$j".mp3
let i++
done
BTW, for those of you who are lucky to have id3 tag attached to their mp3es, there is a program to rename mp3es according to their id3 tag and vice versa.
http://pwp.netcabo.pt/paol/tagtool/
Last edited by Qu3ry; 07-18-2009 at 08:34 AM.
|
|
|
|
07-18-2009, 09:22 AM
|
#28
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Hello Qu3ry
Quote:
Originally Posted by Qu3ry
catkin, I find using files=($(ls *.mp3)) as an array assignment not able to deal with filenames with spaces. Changing it to files=( *.mp3 ) can solve the problem.
|
Thanks for the correction and sorry for the mistake.
Best
Charles
|
|
|
|
07-18-2009, 12:15 PM
|
#29
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
Quote:
Originally Posted by catkin
while IFS=: read userName passWord userID groupID geCos homeDir userShell is perfectly correct but neither transparent nor efficient; its only merit is to save a line. Functionally this is identical
Code:
IFS=:
while read userName passWord userID groupID geCos homeDir userShell
|
I agree with all your other criticisms, but using while IFS=: read userName passWord userID groupID geCos homeDir userShell does have the advantage that IFS gets reset back to previous value after read command finishes.
Quote:
|
Originally Posted by Qu3ry
ntubski, the new script didn't work. There is no mp3 matches the pattern to begin with. I have changed the code to (ls *-*.mp3) to match the pattern and ran the code with the echo command instead of the cat command. It didn't work neither. Here is the result:
*.mp3.mp3 to mp3.mp3
where "to" is the replacement of ">"
|
It works for me:
Code:
~/tmp/songs$ ls
01-1.song.mp3 01-2.song.mp3 01-3.pop.mp3 01-4.pop.mp3
~/tmp/songs$ for song in $(ls *-*.*.mp3 | cut -d. -f2 | sort -u) ; do
> echo *."$song".mp3 to "$song".mp3
> done
01-3.pop.mp3 01-4.pop.mp3 to pop.mp3
01-1.song.mp3 01-2.song.mp3 to song.mp3
|
|
|
|
07-18-2009, 12:22 PM
|
#30
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by ntubski
I agree with all your other criticisms, but using while IFS=: read userName passWord userID groupID geCos homeDir userShell does have the advantage that IFS gets reset back to previous value after read command finishes.
|
Thanks  Interesting! How does that work? AIUI, it would only happen if "while" set up a subshell -- but, if that's the case, how are any variables set in the loop available in the parent shell?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:46 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|