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...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| file.cpp file2.cpp etc I want to remove .cpp Can someone mock up a quick sed commend or some equavalent. Thanks |
|
#2
| |||
| |||
|
for file in `ls *.cpp` do newname=`echo $file|sed 's/\.cpp$//g'` mv $file $newname done #might do it |
|
#3
| |||
| |||
|
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 |
|
#4
| |||
| |||
|
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 |
|
#5
| |||
| |||
|
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 |
|
#6
| |||
| |||
|
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 |
|
#7
| |||
| |||
|
On Nov 12, 4:52*pm, "Chris F.A. Johnson" > 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 |
|
#8
| |||
| |||
|
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 |
|
#9
| |||
| |||
|
puzzlecracker wrote: > On Nov 12, 4:52 pm, "Chris F.A. Johnson" > >>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 |
|
#10
| |||
| |||
|
Le Wed, 12 Nov 2008 14:43:41 -0800, puzzlecracker a écrit*: > On Nov 12, 4:52*pm, "Chris F.A. Johnson" >> 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 |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT -4. The time now is 07:30 PM.




Linear Mode