dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

Remove extension from the file name - shell

This is a discussion on Remove extension from the file name - shell ; Given the list of file file.cpp file2.cpp etc I want to remove .cpp Can someone mock up a quick sed commend or some equavalent. Thanks...


Home > Database Forum > Operating Systems > shell > Remove extension from the file name

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-12-2008, 04:19 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Remove extension from the file name

Given the list of file

file.cpp
file2.cpp
etc

I want to remove .cpp

Can someone mock up a quick sed commend or some equavalent.

Thanks
Reply With Quote
  #2  
Old 11-12-2008, 04:24 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

for file in `ls *.cpp`
do
newname=`echo $file|sed 's/\.cpp$//g'`
mv $file $newname
done

#might do it

Reply With Quote
  #3  
Old 11-12-2008, 04:33 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

Thus spake puzzlecracker (ironsel2000@gmail.com):
> Given the list of file
>
> file.cpp
> file2.cpp
> etc
>
> I want to remove .cpp
>
> Can someone mock up a quick sed commend or some equavalent.


Zsh:
# autoload zmv
# zmv '(*).cpp' '$1'
--
Linux is for people who hate Windows | Christian 'strcat' Schneider
FreeBSD is for people who hate Linux | http://www.strcat.de/
FreeBSD is for people who hate OpenBSD | http://www.strcat.de/blog/
OpenBSD is for people who hate everything | http://strcat.de/chris.gpg
Reply With Quote
  #4  
Old 11-12-2008, 05:51 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

On Wednesday 12 November 2008 21:24, andy wrote:

> for file in `ls *.cpp`


for file in *.cpp

> do
> newname=`echo $file|sed 's/\.cpp$//g'`


newname=`echo "$file"|sed 's/\.cpp$//'`

# or, without external commands
# newname=${file%.cpp}

> mv $file $newname


mv -- "file" "$newname"

> done
>
> #might do it


Reply With Quote
  #5  
Old 11-12-2008, 05:52 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

On 2008-11-12, puzzlecracker wrote:
> Given the list of file
>
> file.cpp
> file2.cpp
> etc
>
> I want to remove .cpp
>
> Can someone mock up a quick sed commend or some equavalent.


Use shell parameter expansion:

file=file.cpp
basename=${file%.cpp}

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
  #6  
Old 11-12-2008, 06:34 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

Le Wed, 12 Nov 2008 12:19:22 -0800, puzzlecracker a écrit*:

> Given the list of file
>
> file.cpp
> file2.cpp
> etc
>
> I want to remove .cpp
>
> Can someone mock up a quick sed commend or some equavalent.


from man basename:
"...If specified, also remove a trailing SUFFIX."

basename *.cpp .cpp


lg
Reply With Quote
  #7  
Old 11-12-2008, 06:43 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

On Nov 12, 4:52*pm, "Chris F.A. Johnson" wrote:
> On 2008-11-12, puzzlecracker wrote:
> > Given the list of file

>
> > file.cpp
> > file2.cpp
> > etc

>
> > I want to remove .cpp

>
> > Can someone mock up a quick sed commend or some equavalent.

>
> * * Use shell parameter expansion:
>
> file=file.cpp
> basename=${file%.cpp}

I like this solution. Would you explain how bash removes the
extension, for it's not quite intuative from the definition above?

Also, I'd like to extend this to any extension type, given everything
that follows the dot is denoted as extension.

THX
Thank you
Reply With Quote
  #8  
Old 11-12-2008, 07:04 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

Le Wed, 12 Nov 2008 22:34:30 +0000, Laurianne Gardeux a écrit*:

> Le Wed, 12 Nov 2008 12:19:22 -0800, puzzlecracker a écrit*:
>
>> Given the list of file
>>
>> file.cpp
>> file2.cpp
>> etc
>>
>> I want to remove .cpp
>>
>> Can someone mock up a quick sed commend or some equavalent.

>
> from man basename:
> "...If specified, also remove a trailing SUFFIX."
>
> basename *.cpp .cpp


Sorry, this is not correct!

should bee:

for i in *; do
basename "$i" .cpp
done

consider the Answer of Chris ...


lg
Reply With Quote
  #9  
Old 11-12-2008, 07:32 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

puzzlecracker wrote:
> On Nov 12, 4:52 pm, "Chris F.A. Johnson" wrote:
>
>>On 2008-11-12, puzzlecracker wrote:
>>
>>>Given the list of file

>>
>>>file.cpp
>>>file2.cpp
>>>etc

>>
>>>I want to remove .cpp

>>
>>>Can someone mock up a quick sed commend or some equavalent.

>>
>> Use shell parameter expansion:
>>
>>file=file.cpp
>>basename=${file%.cpp}

>
> I like this solution. Would you explain how bash removes the
> extension, for it's not quite intuative from the definition above?


See chapter "Parameter Expansion" in man bash.

>
> Also, I'd like to extend this to any extension type, given everything
> that follows the dot is denoted as extension.


$ file=file.cpp
$ ext=cpp
$ basename=${file%.${ext}}
$ echo ${basename}
file


Janis

>
> THX
> Thank you

Reply With Quote
  #10  
Old 11-12-2008, 10:18 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Remove extension from the file name

Le Wed, 12 Nov 2008 14:43:41 -0800, puzzlecracker a écrit*:

> On Nov 12, 4:52*pm, "Chris F.A. Johnson" wrote:
>> On 2008-11-12, puzzlecracker wrote:
>> > Given the list of file

>>
>> > file.cpp
>> > file2.cpp
>> > etc

>>
>> > I want to remove .cpp

>>
>> > Can someone mock up a quick sed commend or some equavalent.

>>
>> * * Use shell parameter expansion:
>>
>> file=file.cpp
>> basename=${file%.cpp}

> I like this solution. Would you explain how bash removes the extension,
> for it's not quite intuative from the definition above?
>
> Also, I'd like to extend this to any extension type, given everything
> that follows the dot is denoted as extension.


for i in *; do echo "${i%.*}"; done

lg
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 07:30 PM.