LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-05-2006, 09:43 PM   #1
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 62
'ls' .. or something to identify filenames that have a (space)(numeral) pattern


I have a bunch of files named ...file 7.mp3, file 8.mp3, file 9.mp3, file 10.mp3, file 11.mp3 ... etc.

I want to find a way to list only those files that have a 1 digit numeral, so I can write a script to rename them to ~07.mp3, ~08.mp3, etc. for better sorting.

I've been messing around with ls for quite a while without getting it right.
 
Old 05-05-2006, 10:07 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I had to do a bit of this myself a while back. I used rename to do the work:
Code:
rename " " " 0" *\ [0-9].mp3
That command assumes a space occurs immediately before the single-digit number and that there is only one space in the filename.

Of course, you can replace the "mp3" with another * if you have other extensions, but be careful... it could throw things off if any of the filenames have periods as part of the name.

Last edited by Dark_Helmet; 05-05-2006 at 10:13 PM.
 
Old 05-05-2006, 10:16 PM   #3
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Original Poster
Rep: Reputation: 62
Thanks a lot. The "[0-9]" was the part I couldn't get right. Should have thought about grep syntax. My files have more than one space in the name, but I know how to get the script done now.
 
Old 05-06-2006, 03:46 AM   #4
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Original Poster
Rep: Reputation: 62
Still troubled

Well, I thought I knew how to do this... All the files I'm interested in are in a 'nested' directory. Let's say DirLevel0, DirLevel1, and DirLevel2. The actual files are all the bottom directory.

Here's my script:
Code:
#!/bin/sh
SONG=0
ALBUMTITLE="Album01"
for file in DirLevel0/*/*/*\ [0-9].mp3
  do ALBUMKEY=${file:38:7}	
     if test $ALBUMTITLE=$ALBUMKEY; then SONG=$(($SONG+1))
       else SONG=1
     fi
     ALBUMTITLE=$ALBUMKEY
     file1=${file//\ ?.mp3/\ 0$SONG.mp3}
#     echo $file
#     echo $file1
#     mv "$file" "$file1"
  done
The problem seems to be in the 'if' statement. All the correct files are being selected, but when the 'ALBUMKEY' changes, the counter (SONG) keeps on incrementing. It never goes back to 1.

Just in case it's not obvious, I am a real novice at scripting, and I hope someone can point out my obvious error while I'm sleeping. Thanks.
 
Old 05-06-2006, 06:15 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I changed the script to match up with my regular formatting...

Code:
#!/bin/sh

SONG=0
ALBUMTITLE="Album01"

for file in DirLevel0/*/*/*\ [0-9].mp3
do
  ALBUMKEY=${file:38:7}	

  if [ "${ALBUMTITLE}x" = "${ALBUMKEY}x" ] ; then
    ((SONG=SONG+1))
  else
    SONG=1
  fi

  ALBUMTITLE=${ALBUMKEY}
  file1=${file//\ ?.mp3/\ 0$SONG.mp3}
#     echo $file
#     echo $file1
#     mv "$file" "$file1"
done
The script seems to work as I expect, but I don't know exactly what the file setup is.

The SONG counter will reset when ALBUMKEY changes. So double-check that your ALBUMKEY assignment is doing what you expect. Does the text you want for ALBUMKEY always start at the 38th character (including the path)?

Last edited by Dark_Helmet; 05-07-2006 at 12:15 AM.
 
Old 05-06-2006, 11:47 AM   #6
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Original Poster
Rep: Reputation: 62
Quote:
Does the text you want for ALBUMKEY always start at the 38th character (including the path)?
Yes, but that will change whan I get out of my testing environment, and into the real directories. With some very minor twiddling, this script is now working. There does seem to be an extraneous "x" in your 'if test' statement. Thanks again.

What I'm working on is converting a 64 cd set of the King James Bible. I'm only about half way thru the process right now. Since you've looked at this a couple times, maybe you (or someone) could suggest a means of getting the leading 0 in there from the beginning. I am ripping the files to .wav, then running this script to encode and name them.
Code:
#!/bin/sh
MP3NO=1
for file in tmp/*.wav
    do lame -m m -V 7 "$file" "tmp/Holy Bible (KJV-Jon Sherberg) OT20 Proverbs $MP3NO.mp3"
    MP3NO=$(($MP3NO+1))
done

Last edited by rickh; 05-06-2006 at 04:22 PM.
 
Old 05-07-2006, 12:23 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Actually, there was a missing 'x' in the if statement Which I've now added. Adding the 'x' on each argument guarantees no mysterious comparison failures. That is, if ALBUMTITLE or ALBUMKEY ever evaluate to an empty string, the comparison won't croak. Without the 'x' (or some other letter/number), an empty string will cause the script to fail with an "expected two arguments" error or something similar.

As for the second script, that's easy. You just need a new if statement and another variable:
Code:
#!/bin/sh

MP3NO=1
for file in tmp/*.wav
do

  if [ ${MP3NO} -lt 10 ] ; then
    MP3NO_TEXT="0${MP3NO}"
  else
    MP3NO_TEXT="${MP3NO}"
  fi

  lame -m m -V 7 "$file" "tmp/Holy Bible (KJV-Jon Sherberg) OT20 Proverbs $MP3NO_TEXT.mp3"

  MP3NO=((MP3NO+1))

done
There may be a more elegant way of doing the same thing, but that works.

Last edited by Dark_Helmet; 05-07-2006 at 12:25 AM.
 
Old 05-09-2006, 01:51 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I recommend using
if [[ <yourtest> ]]
syntax ie double [[ ]], it's more predictable/resilient
see this page: http://www.tldp.org/LDP/abs/html/testconstructs.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
identify hard drive space milesstandish Linux - Hardware 6 03-19-2004 06:12 PM
\ before space in filenames using gnome terminal ? cozye Linux - General 1 12-09-2003 01:44 PM
'ls' command... sramelyk Slackware 7 10-01-2003 12:45 PM
replacing space in filenames rooster13 Linux - General 2 07-18-2003 04:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration