Stripping out the extension of a file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping out the extension of a file name
# 1  
Old 12-27-2006
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 is appreciated.
thanks
ram
# 2  
Old 12-28-2006
Hi ramky79, the FAQ submission queue exists so that a new FAQ can be submitted to the moderators/admins for review and upload into the FAQ section. Please do not post any queries in that.

Regarding your question:

Code:
# filename=myfile.txt
# newvariable=$(echo ${filename%.*})
# echo $newvariable
myfile

Make sure that you use ksh or bash for this. For explanations, read the Parameter Expansion section in the man page for ksh or bash.
# 3  
Old 12-28-2006
Quote:
Originally Posted by blowtorch
Code:
# filename=myfile.txt
# newvariable=$(echo ${filename%.*})
# echo $newvariable
myfile

Blowtorch, why the extra subshell and echo, the holiday running on a bit? Smilie

Code:
# filename=myfile.txt
# newvariable=${filename%.*}
# echo $newvariable

# 4  
Old 12-28-2006
My bad! But in my defence, I've been up since 0445! That leaves me only half-awake for the rest of the day!
-EDIT
It's 0932 now
-/EDIT

Last edited by blowtorch; 12-28-2006 at 09:32 PM..
# 5  
Old 12-28-2006
you can use basename too
Code:
basename "file.txt" .txt

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