![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Stripping out extension in file name | devs | Shell Programming and Scripting | 9 | 05-14-2008 01:53 AM |
| Truncate multiple file extensions | prvnrk | Shell Programming and Scripting | 12 | 04-04-2008 07:20 AM |
| Stripping out the extension of a file name | ramky79 | Shell Programming and Scripting | 2 | 12-27-2006 10:25 AM |
| find -regex: matching multiple extensions | r0sc0 | Shell Programming and Scripting | 2 | 12-08-2005 10:32 AM |
| stripping last lien off a file | vivekshankar | UNIX for Dummies Questions & Answers | 3 | 05-31-2005 03:35 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Stripping out extensions when file has multiple dots in name
I posted this already in another thread, but was told that I should create a seperate thread for the following question:
How do I strip the extension when the delimiter might occur multiple times in the filename? For example: I have 2 files as input for my script. test.extension test.foo.extension In my script I want to see "test" and "test.foo" as results. But the following script-snippet gives "test" for both files. I know this is caused by the -f1 that I use as setting for cut. But I want to know which command I should use so it starts looking for the delim-character from the right i.s.o the left (probably a different command than cut, but which?) Code:
!/bin/sh
FILE_NAME=$1
if [ ! -r ${FILE_NAME} ]
then
echo "Could not find file ${FILE_NAME}"
exit 1
fi
FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'`
echo "FNAME = ${FNAME}"
TIA |
| Forum Sponsor | ||
|
|
|
|||
|
Quote:
After checking with a coleague who has used sed before in the past I understand what the regular expresion means: "replace s with any number of characters followed by a dot followed by any number of characters and store the first set of any number of characters". What he could not explain to me is why it finds the last dot in test.foo.extension and not the first dot (thus why it actually works). Since both "any number of characters" may contain dots (in theory) if I understand the man-pages (and his explanation) correctly. Anyway since ".extension" is a fixed extension he came up with another solution (after reading your solution), which also works when the extension is not there (by accident) : Code:
FNAME=echo "${FILE_NAME}"|sed 's/\(.*\)\.program/\1/'
Last edited by Nemelis; 05-14-2008 at 04:10 AM. Reason: small typo |