Stripping out extension in file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping out extension in file name
# 1  
Old 02-25-2007
Stripping out extension in file name

This command gives me just the filename without any extension:
evrvar =`echo filename.tar | sed 's/\.[^.]*$//'`

I am trying to make a change to this command... to make it work for... filename.tar.gz to get just the filename....

currently the command gives me filename.tar by removing only gz... I am new to regular expression... pls help.

Thx.
# 2  
Old 02-25-2007
try this:
Code:
echo filename.tar.gz |awk -F. '{ print $1 }'

or
Code:
echo filename.tar.gz |cut -d'.' -f1

or sed solution
Code:
echo "filename.tar.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'

or
Code:
echo "filename.tar.gz" | sed 's/.......$//g'


Last edited by ahmedwaseem2000; 02-25-2007 at 05:08 AM..
This User Gave Thanks to ahmedwaseem2000 For This Post:
# 3  
Old 02-25-2007
If filename does not contain period(s):

Code:
$ fname="filename.tar.gz"
$ echo "${fname%%.*}"
filename


Otherwise:

Code:
${fname%.*.*}


Last edited by radoulov; 02-25-2007 at 08:15 AM..
# 4  
Old 02-25-2007
Thanks for the replies.

can this regular expression be made possible to use for both filename.gz & filename.tar.gz

I want to get filename for both of these commands as just filename only:
echo "filename.tar.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'
echo "filename.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'

thanx in advance.
# 5  
Old 02-25-2007
You could use either the awk or the cut solutions they will work in all the scenarios. Here is the sed solution to generalize on both the occasions.

Code:
 echo "filename.tar" | sed "s/\([^.*]\)\.\(.*\)*$/\1/g"


Last edited by ahmedwaseem2000; 02-25-2007 at 12:09 PM..
# 6  
Old 02-25-2007
Quote:
Originally Posted by ahmedwaseem2000
You could use either the awk or the cut solutions they will work in all the scenarios. Here is the sed solution to generalize on both the occasions.

Code:
 echo "filename.tar" | sed "s/\([^.*]\)\.\(.*\)*$/\1/g"

echo "filename.tar" | sed "s/\..*$//"
# 7  
Old 02-26-2007
thank you all.. it was helpful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

3. Shell Programming and Scripting

Stripping ret of the lines in a file (sed question)

Hi all, I didn't use SED for 20 years and was never an expert. So my current knowledge is about zero. Please be patient with me. I'm neither a native speaker. I have a huge dictionary file and want the rest of the lines stripped. Everything after (and including) the "/" should be stripped. I... (2 Replies)
Discussion started by: Hinnerk2005
2 Replies

4. Shell Programming and Scripting

Stripping characters from a file and reformatting according to another one

Dear experts, my problem is pretty tricky. I want to change a file (see attached input.txt), according to another file (help.txt). The output that is desired is in output.txt. The example is attached. Note that -dashes should not be treated specially, they are considered normal characters,... (2 Replies)
Discussion started by: TheTransporter
2 Replies

5. Homework & Coursework Questions

Help with file stripping shell script

Well my last assignment came and i tried some things but i guess it's pretty puzzling for me to figure out all the points of the problem. I was able to achieve something but it's not complete. 1. The problem statement, all variables and given/known data: Write a bash program which strips the... (5 Replies)
Discussion started by: Florinel76
5 Replies

6. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

7. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: Nemelis
8 Replies

8. 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

9. Shell Programming and Scripting

Stripping out the extension of a file name

I have two variables to be dynamically defined in my shell script variable1= myfile.txt variable2= myfile The second variable depends on the first ( i mean , it is a part of the first variable) Now, I need to strip out the ".txt" part from the first variable how do i do that in a shell script. (2 Replies)
Discussion started by: ramky79
2 Replies

10. UNIX for Dummies Questions & Answers

stripping last lien off a file

Hi, how can i strip the last line off my file using shell script? Thanks and Regards Vivek.S (3 Replies)
Discussion started by: vivekshankar
3 Replies
Login or Register to Ask a Question