LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-12-2011, 06:07 AM   #1
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
string substitution


just look into the following code...


Code:
#!/bin/sh
set -x


if [ $# -ne 3 ] 
then
        echo "Usage : $(basename $0) <path> <from ext> <to ext>" 
        exit 0
fi

direct=${1}
from_ext=.${2}
to_ext=${3}

for file in ${direct}/*
do
        filename=${file%%'${from_ext}'}
        if [ ${filename} != $file ]
        then
                mv ${file} ${filename}.${to_ext}
        fi  
done

the code is supposed to move all files with first extension(2nd arg) to the 2nd extension(3rd arg).

Wht's wrong in the code ??
 
Old 01-12-2011, 08:18 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,506

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Please explain the following line:
Code:
filename=${file%%'${from_ext}'}
 
Old 01-12-2011, 08:27 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
BTW: there is also the command rename, which can rename files based on a pattern.
 
Old 01-12-2011, 11:06 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
BTW you don't need any of the { } around variable names except for the outermost in ${file%%'${from_ext}'}.

Grail's question will lead to a solution.
 
Old 01-12-2011, 11:50 AM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I don't like string substitution syntax, it looks obscure, instead I would use basename in this way:

Code:
mv "$i" "$(basename "$i" .avi).mkv"
from .avi to .mkv, real simple and readable, this is what I always use.

Now, of course, I don't use this to batch rename things, I use rename. Instead I use this when converting formats with ffmpeg.
 
Old 01-12-2011, 02:10 PM   #6
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
Quote:
filename=${file%%'${from_ext}'}
it will remove the content of from_ext at the end of file
 
Old 01-12-2011, 02:23 PM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
Did you test this expression on the command line and got the desired result?
 
Old 01-12-2011, 02:54 PM   #8
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
nop...i am confused..
 
Old 01-12-2011, 03:26 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by lipun4u View Post
it will remove the content of from_ext at the end of file
As a further hint you might want to read up about the difference of single and double-quotes when using bash.
 
Old 01-12-2011, 06:34 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,506

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Ahhhh ... crts with the spoiler
 
Old 01-12-2011, 08:12 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Smile It wasn't a total spoiler ...

... I merely gave a hint and which will also help the OP with the other problematic lines.
I'm just saying: whitespace

So there is still plenty of fun left for the OP
 
Old 01-13-2011, 01:47 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,506

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Nice work as always crts
 
  


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
string substitution lipun4u Linux - Newbie 3 02-22-2010 02:19 AM
Help with a sed substitution string hawgwild Programming 2 09-24-2009 02:35 PM
String Substitution Swapna173 Linux - Newbie 1 08-04-2008 06:22 AM
Substitution of a long string horacioemilio Programming 5 04-07-2008 09:21 PM
Complicated string substitution horacioemilio Programming 5 02-13-2008 06:40 PM


All times are GMT -5. The time now is 02:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration