LinuxQuestions.org
Help answer threads with 0 replies.
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 12-10-2009, 09:19 PM   #16
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70

Quote:
Originally Posted by ghostdog74 View Post
basename works on one file and AFAIK does not support wildcard (if it does, correct me). To work on multiple files , effectively a loop is still required. In that case, its the same as calling external command for each file.
Just giving another option
 
Old 04-21-2012, 04:34 PM   #17
Pampano
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Rep: Reputation: Disabled
Kinda have a similar question and found this thread. What if I had many files but needed to change the extensions of them and they all have different extensions.
For example, if I were to use an sed command to change the file "batman.CPP" to be "batman.cpp"
And I also have a file "kingsandqueens.dat" and I need it to be "kingsandqueens.backup"


I need a shell script that will do this for me if I were to execute the program as ./chExt1.sh 'cpp' 'batman.CPP'
or ./chExt1.sh 'backup' 'kingsandqueens.dat'

Thanks for any help with this.

Last edited by Pampano; 04-21-2012 at 04:40 PM.
 
Old 04-21-2012, 08:01 PM   #18
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Quote:
Originally Posted by ghostdog74 View Post
basename works on one file and AFAIK does not support wildcard (if it does, correct me). To work on multiple files , effectively a loop is still required. In that case, its the same as calling external command for each file.
True ghostdog74. I was merely giving an example of how to remove the file extension. Of course this would have to be used in a loop to modify a whole list of filenames.
 
Old 04-22-2012, 03:20 AM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1)
To those who haven't noticed, this is a 3 year old thread that has been reopened. It's usually a good idea to not post to old threads unless you specifically have something important to add to the previous discussion. New questions should be made in new threads, perhaps with a referral to the old one if relevant.

2)
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

3)
When you post, please give us all the relevant details about what you are trying to do. What platform are you using, where are you getting your data, what format is it in, etc.

For example, the old discussion focused on the the occurrence of '\r' in the string. You'll generally only see that as the result of dos-style line endings when operating on files written in Windows. So I suspect the filenames were taken from a windows-created text file, rather than directly. A simple conversion of the file first, or stripping off the last character, would fix that.

4)
For pampano, there are many string manipulations that can be done with shell built-in commands, so probably you won't need to call on sed or another external command.

parameter substitution
string manipulation

And again, you need to define your requirements clearly. Do you just need to lowercase the whole variable? Or ensure that only the extension is lowercase? To use recent bash for that:

Code:
filename='Foobar.TXT'

echo "${filename,,}"		#lowercases the entire filename

ext=${filename##*.}		#grabs the extension from the filename

echo "${filename%.*}.${ext,,}"	#echos the filename without extension,
				#plus lowercased extension.
As for changing the extension, All you need to know has been presented already:

Code:
filename='kingsandqueens.dat'
echo "${filename%.*}.backup"

Last edited by David the H.; 04-22-2012 at 03:22 AM.
 
1 members found this post helpful.
  


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
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
sed command to replace date format 2007/01/22 with 070122? johnpaulodonnell Linux - Newbie 3 01-23-2007 07:29 AM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:30 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
Open Source Consulting | Domain Registration