New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Newbie help with bash script to search and replace a filename


Post Reply New Thread Subscribe

 
Thread Tools
gilc

 
Member Since: Sep 25, 2007
Posts: 12
gilc is on a distinguished road

gilc is offline
I'm a complete newbie to shell/bash scripting (only been going a couple of days) and am trying to get to grips with it.

I'm trying to create a script that loops through a folder finding any sub folders that are called Season 1-9 and appends a 0 to the beginning of the number.

E.g Season 1 -> Season 01
Season 2 -> Season 02
Season 11 -> Season 11

I think this code is right for looping through find only folders and leaving files alone. Just can't seem to work out how to use sed and regular expressions to replace the text in the file names.

Here's my code so far and would love some pointers.

Code:
#!/bin/bash
# Script to pad a 0 to Season numbers

TV_DIR=~/Desktop/TV

cd "$TV_DIR"

for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders 
	
	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
	echo "$dir"

	
	done

echo "Job Done."
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Here is the easiest sample I could think of.

Code:
echo "Season 11" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
gilc

 
Member Since: Sep 25, 2007
Posts: 12
gilc is on a distinguished road

gilc is offline
Thanks for that xstep.

It seems to be half working. It's certainly replacing the text as I want and even echoing it back to the screen but I've tried to apply the code to a variable and then use this with a mv command to change the the filename. E.g.

Code:
mv $dir $newfilename
Code:
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
Job Done.
But it doesn't seem to be working and instead only outputting an error on how to use the mv command. When I try to echo the $newfilename variable this just shows up as empty.

Here's my script now and would be interesting to know what the sed stuff does that you suggested as I don't fully understand the line of code you've written.

Code:
#!/bin/bash
# Script to pad a 0 to Season numbers

TV_DIR=~/Desktop/TV

cd "$TV_DIR"

for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders 
	
	newname=$("$dir" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/')
	echo "This is the new folder name $NEWNAME"
	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
	
	mv $dir $newname 	
	
	done

echo "Job Done."
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Here is my version...

Code:
#!/bin/bash
# Script to pad a 0 to Season numbers

TV_DIR=~/Desktop/TV

cd "$TV_DIR"

#loop through each entry in the TV_DIR finding folders 
#remove spaces because for loop uses them as name delimeters.
for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do

        #remove my special characters.
        dir=`echo $dir | sed 's/~~/ /'`
        #change single digit to two digits.
	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`
	echo "This is the new folder name $newname"
	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc

#mv folderA folderA seems to have special meaning in mv command so bypass those same name cases.
	if [ "$newname" != "$dir" ]; then
	    mv "$dir" "$newname"
	fi

	done

echo "Job Done."

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
gilc

 
Member Since: Sep 25, 2007
Posts: 12
gilc is on a distinguished road

gilc is offline
Quote:
Originally Posted by xstep View Post
Here is my version...

Code:
#!/bin/bash
# Script to pad a 0 to Season numbers

TV_DIR=~/Desktop/TV

cd "$TV_DIR"

#loop through each entry in the TV_DIR finding folders 
#remove spaces because for loop uses them as name delimeters.
for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do

        #remove my special characters.
        dir=`echo $dir | sed 's/~~/ /'`
        #change single digit to two digits.
	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`
	echo "This is the new folder name $newname"
	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc

#mv folderA folderA seems to have special meaning in mv command so bypass those same name cases.
	if [ "$newname" != "$dir" ]; then
	    mv "$dir" "$newname"
	fi

	done

echo "Job Done."
AMAZING xstep. That works exactly as I intended/wanted.

But what do these two lines do exactly?

Code:
dir=`echo $dir | sed 's/~~/ /'`
	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
The echo command pipes $dir to the sed command which does it's magic. You had "$dir" by it self which for some reason was messing me up.

The lines are separated to make it easier for me to understand. The first one undoes what I did in the find statement. The second does what your original intent was.

Do a 'man sed' to see how the -e helps us.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« Auto Generated Application List | Terminal won't start up »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
how can i search and replace email addresses in a text file? lysanderspooner OS X - Apps and Games 3 05-10-2009 02:13 AM

All times are GMT -4. The time now is 02:23 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?