Getting the file extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the file extension
# 1  
Old 04-26-2011
Getting the file extension

I have a file

Code:
n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod

and I want to get the extension.

At the moment I have

Code:
set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'`

which of course does not work as there is a point in varp0.25
# 2  
Old 04-26-2011
what shell? modern bash:

Code:
var=n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod
echo ${var##*.}
cmod

# 3  
Old 04-26-2011
If your shell has variable substitution, and assuming the extension is supposed to be "cmod"

Then this ought to work:

Code:
 
my_file=n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod
my_ext=${my_file##*.}
echo $my_ext
cmod

# 4  
Old 04-26-2011
I am using csh

I need both the filename and the extension. At the moment it is as below

Code:
set filename = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'`

# 5  
Old 04-26-2011
Code:
#!/bin/csh
set pathvar=/home/WSJ091305.txt
echo $pathvar:r
echo $pathvar:h
echo $pathvar:t
echo $pathvar:e

Code:
#
# The result of executing this script is:
#
/home/WSJ091305
/home
WSJ091305.txt
txt

# 6  
Old 04-26-2011
You want the last field, which in awk is referenced by $NF.

Regards,
Alister
# 7  
Old 04-26-2011
I have done the below to get the file extension.

Code:
  set filextension = `echo $f | awk 'BEGIN {FS="."} {print $NF}'`

But what should I do to get the file name?
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

File Extension Missing in file-$var.csv

I'm afraid this is a silly question but I can't figure it out. I have a script like so... echo "Enter DRDL Signature Version Number" read DRDL_Number mv signature_output.csv SERVICE_OBJECTS_S-$DRDL_Number.csv The resultant filename does not contain the .csv as follows.... (3 Replies)
Discussion started by: Cludgie
3 Replies

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

4. Shell Programming and Scripting

Rename file extension.

I have a list file that contains names of many files. I am reading one file name at a time using for loop Then I like to create one more list file but with the file extension changed to "ctl". Note: The file name can have any number of dots ".". But the extension after the last dot should be... (4 Replies)
Discussion started by: pinnacle
4 Replies

5. Shell Programming and Scripting

for loop other file with same name and different extension

Hi Friends, I am using the following command for i in `ls $PWD`; do cat $i > test && mv test $i; done But, I want to execute another command and write the output to another file with different extension but same name, like this I tried using this for i in `ls $PWD`; do... (2 Replies)
Discussion started by: jacobs.smith
2 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. UNIX for Dummies Questions & Answers

About the file extension

Hi everyone, Can we know that which type of file is there without opening a file thanks in advance.... kunal patil (3 Replies)
Discussion started by: kunalpatil09
3 Replies

8. Shell Programming and Scripting

search for a file extension

hi all, i'm new to shell scripting, i need help from u guys to do my task now.. i just need to check a file extension existence in a directory, and if it exists then i have to continue my processing. pls give me the command to check the extension of the files (6 Replies)
Discussion started by: divak
6 Replies

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

10. 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
Login or Register to Ask a Question