Stripping out extensions when file has multiple dots in name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping out extensions when file has multiple dots in name
# 1  
Old 05-14-2008
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
# 2  
Old 05-14-2008
Try basename
# 3  
Old 05-14-2008
Hi

Instead of FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'` , try the below line



FNAME=`echo "${FILE_NAME}"| sed 's#^\(.*\)\.\(.*\)#\1#' `

Thanks
Penchal
# 4  
Old 05-14-2008
Try this:

Code:
echo 'test.foo.extension' | sed 's/\(.*\)\..*/\1/'

Regards
# 5  
Old 05-14-2008
Quote:
Originally Posted by Franklin52
Try this:

Code:
echo 'test.foo.extension' | sed 's/\(.*\)\..*/\1/'

Regards
Thanx.

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/'

(The actual "extension" is ".program")

Last edited by Nemelis; 05-14-2008 at 08:10 AM.. Reason: small typo
# 6  
Old 05-14-2008
The asterisk will prefer "longest leftmost" so that decides on which side any "extra" dots will go.

If the extension name is always the same, I'll say "basename" again.
# 7  
Old 05-14-2008
Quote:
Originally Posted by Nemelis
Code:
FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'`

echo can do the job, anything else is useless.
Code:
FNAME=`echo "${FILE_NAME%.*}"`

Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get file extension with multiple dots

I am trying to get the file extension with file names that could contain multiple dots using shell scripting. I want to find a way using the sed command. Example Filenames: one.dat one.dat.002 Results: dat I would like to return dat in both instances using the sed command. How can I... (4 Replies)
Discussion started by: smkremer
4 Replies

2. Shell Programming and Scripting

List directory name (only once) after multiple file extensions found

Here is a simplified example of my problem. Say I have the following 3 sub-directories; ./folder1 A.txt A.sh ./folder2 B.txt ./folder3 C.txt C.sh I would like to list the directory names which contain both '.txt' & '.sh' type extensions. I have came up with the following code;... (8 Replies)
Discussion started by: mmab
8 Replies

3. Shell Programming and Scripting

Remove filenames beginning with multiple dots

hi all, I want to remove filenames beginning with multiple dots.how I can do this. Thanks in advance (5 Replies)
Discussion started by: sriharsharavi
5 Replies

4. Shell Programming and Scripting

Replacing multiple extensions

HI, I have some csv files with mutiple extensions, I want to remove all the extensions and keep only the .csv extension. anybody can suggest me how to do this. source files 1.txt.csv.txt.csv.csv.txt.csv 2.csv.txt.csv.txt.csv.txt target 1.csv 2.csv --Wang (1 Reply)
Discussion started by: wangkc
1 Replies

5. Shell Programming and Scripting

Replace multiple dots (.) with spaces ( )

Hi all, I have files in the filename pattern of, this.is.the.name.of.my.file.mov and I would like to remove dots (.) and replace them with spaces ( ) so the output would be, this is the name of my file.mov The other issue that I have is that the number of dots (.) in the file... (6 Replies)
Discussion started by: Monkey Dean
6 Replies

6. UNIX for Dummies Questions & Answers

changing extensions for multiple files at once

I copied some files to another folder, and I want to change them from .doc extensions to .txt extensions. I tried using the cp and mv commands, but it didn't work. Is it possible to change file extensions with these commands, and if so how do I do it? I tried using the * wildcard (say cp *.doc... (1 Reply)
Discussion started by: Straitsfan
1 Replies

7. Shell Programming and Scripting

Creating multiple extensions

Friends I want to automat sending a letter to different persons whose directories are named as 001, 002, 003, 004. To push the same letter to all these directories, I need to name the letter as letter.001, letter.002 like that. Is there any method whereby I get the extension of this letter... (2 Replies)
Discussion started by: chssastry
2 Replies

8. Shell Programming and Scripting

Truncate multiple file extensions

Hi, I have files with names like file1.txt.txt.txt.txt and file2.txt.txt.txt.txt.txt............ (random infinite number of .txt exist). how to truncate (mv) their names to ones with single .txt extension like file1.txt and file1.txt ? In other words, how to extract the filename upto first... (12 Replies)
Discussion started by: prvnrk
12 Replies

9. Shell Programming and Scripting

Stripping out the extension of a file name

I have written a shell script and in my script i have a variable filename=myfile.txt now, i want another variable to be defined for which i have to strip out the extension fo the file name, i.e. newvariable= myfile how do i strip out the ".txt" part from my first variable. Any kind of help... (4 Replies)
Discussion started by: ramky79
4 Replies

10. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies
Login or Register to Ask a Question