LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-30-2011, 02:58 PM   #16
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0

Thank you so much for helping me out.
I found something about parameter substitution a while back, but it was outdated and quite hard for me to understand, but now that i see this it makes more sense.

Tomorrow I've got some more free time to try

Thanks allot, i appreciate it!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-01-2011, 04:52 AM   #17
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
OK, the script you posted is undergoing mayor maintenance
I now see how the parameter substitution works, if you do it a couple times, it's quite easy !

Quote:
#using default folder when no parameters given
DIR=${1:-/home/lucas/Music/untitled folder/*.mp3}

shopt -s extglob #enable extended globbing

for file in ${DIR} ; do
I used a default dir, since it's that's the mostly used place.

Quote:
~/Music $ ./renaming "untitled folder"
.---------------------------------------- - - - - -- - - - - -- - - - - - - - -
| + Filename: untitled folder
| + Changing: "untitled_folder"
'---------------------------------------- - - - - -- - - - - -- - - - - - - - -
~/Music $ ./renaming
.---------------------------------------- - - - - -- - - - - -- - - - - - - - -
| + Filename: 202_wiz_khalifa_and_t-pain-black_&_yellow_-_(remix)-(82_bpm)-zzzz.mp3
| + Changing: "wiz_khalifa_&_t-pain-black_&_yellow-(remix).mp3"
'---------------------------------------- - - - - -- - - - - -- - - - - - - - -
Quote:
#replace delimiter
newname=${newname//+(_-_)/-}

#replace all instances
newname=${newname//+(_and_)/_&_}


#replacing featuring tags
newname=${newname//_feat._/_ft._} #it ain't possible to replace multiple tags?
#i.e. ${newname//_feat._\|_feat_\|_f._\|_ft_/_ft._}
I wanted to know whether it's possible or not to replace multiple instances using character substitutions? (this obviously doesn't work, but i cannot find it anywhere)

Did quite some reading and hereby i want to thank you again for all the lessons and literature
If i look at me old script's code it looks like garbage now haha.
Regards

Last edited by mainstream; 04-01-2011 at 04:59 AM.
 
Old 04-01-2011, 09:22 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
I would suggest placing the alternatives in a string which can be edited in one place:
Code:
regex='_feat._|_ft_'
newname=${newname//+($regex)/_ft._}
I would also suggest a slight change to the following:
Code:
#using default folder when no parameters given
DIR=${1:-/home/lucas/Music/untitled folder/*.mp3}

shopt -s extglob #enable extended globbing

for file in ${DIR} ; do
The issue I see is that DIR implies directory and as such when the user uses the script like so:
Code:
./renaming "untitled folder"
The previous code will set DIR to "untitled folder" and the for loop will now look at all files in the directory, not just mp3s

Your options could be to either add a second parameter with the extension you would like or add a static variable that you can alter manually, like so:
Code:
#using default folder when no parameters given
DIR=${1:-/home/lucas/Music/untitled folder}

#default file type
TYPE='.mp3'

shopt -s extglob #enable extended globbing

for file in ${DIR}/*$TYPE ; do
 
Old 04-01-2011, 11:50 AM   #19
toordog
Member
 
Registered: Jul 2010
Location: Montreal, Canada
Distribution: RedHat, Ubuntu, Solaris, AIX, BSD
Posts: 42

Rep: Reputation: 3
Wrong thread post.
DELETED

Last edited by toordog; 04-01-2011 at 12:30 PM. Reason: post in the wrong thread.
 
Old 04-01-2011, 12:23 PM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
@toordog - ummm ... did you post in the wrong question? If not it is considered a little rude to hijack someone else's question, especially with an unrelated question.
 
Old 04-01-2011, 12:29 PM   #21
toordog
Member
 
Registered: Jul 2010
Location: Montreal, Canada
Distribution: RedHat, Ubuntu, Solaris, AIX, BSD
Posts: 42

Rep: Reputation: 3
Quote:
Originally Posted by grail View Post
@toordog - ummm ... did you post in the wrong question? If not it is considered a little rude to hijack someone else's question, especially with an unrelated question.
It's totally posted in the wrong thread. I just noticed it. I will move it.
 
Old 04-01-2011, 01:33 PM   #22
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I'm glad you're getting the hang of it.

Yes, as grail shows, another benefit of extended globbing is the ability to match multiple patterns. And storing long/complex strings in variables often makes the code more readable and easier to handle overall, particularly if you're likely to edit them often.

However, you probably don't need to use extglob in cases like these:
Code:
newname=${newname//+(_-_)/-}

newname=${newname//+(_and_)/_&_}
The default ${var//x/y} already handles multiple instances in a string. The +(x) is only necessary when the number of consecutive instances of the pattern is unknown, and you need to match the whole thing.
Code:
$ x='foo foofoo foofoofoo'

$ echo "${x//foo/bar}"
bar barbar barbarbar

$ echo "${x//+(foo)/bar}"
bar bar bar
Oh, and incidentally...
Code:
$ x='foo foofoo foofoofoo bar barbar barbarbar'

$ echo "${x//+(foo|bar)/baz}"
baz baz baz baz baz baz

$ echo "${x//@(foo|bar)/baz}"
baz bazbaz bazbazbaz baz bazbaz bazbazbaz

$ echo "${x//*(foo|bar)/baz}"
bazbaz bazbaz bazbaz bazbaz bazbaz baz

$ echo "${x//?(foo|bar)/baz}"
bazbaz bazbazbaz bazbazbazbaz bazbaz bazbazbaz bazbazbaz

$ echo "${x//!(foo|bar)/baz}"
baz
I'm not entirely sure what's going on in the last three. Pattern matching "zeros" and "nots" can be quite confusing.
 
Old 04-07-2011, 06:25 AM   #23
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Thanks all :-)

This is something i can work with!

Haven't been working on it since the last reply.
Had some health problems, which won't let me and consume most of my free time lately.

Anyways, i will pick up soon i hope.

Thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
A distro called `Enigma` jago25_98 Linux - Distributions 2 03-29-2011 05:05 AM
Enigma Napalm Llama Linux - Games 0 11-13-2004 04:53 PM
Redhat 7.1 enigma vicente Linux - Distributions 2 05-15-2002 03:56 PM
redhat 7.2 enigma vicente Linux - Distributions 3 04-28-2002 08:27 AM
recommendations for RH enigma? Serpent Goddess Linux - Distributions 7 03-08-2002 12:09 PM


All times are GMT -5. The time now is 04:57 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration