Get file extension with multiple dots


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get file extension with multiple dots
# 1  
Old 03-02-2012
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 achieve this?

sed 's/^.*\.//' = just returns the last three (dat and 002 respectively)
sed 's/\(.*\)\..*/\1/' = returns one and one.dat respectively

Any help is greatly appreciated!!!

Thanks!
# 2  
Old 03-02-2012
Code:
MYFILE="one.dat.002"
t=${MYFILE#*.}
echo "${t%%.*}"

# 3  
Old 03-02-2012
Can I delete a post?

Last edited by gary_w; 03-02-2012 at 02:15 PM..
# 4  
Old 03-02-2012
I am new to this type of programming so I don't know the full details. I currently have a job in Appworx (UC4 Job Scheduling) that calls a shell script (#!/bin/sh) which processes information (copying, removing files, etc). Instead of hardcoding .dat (in case later on it becomes .DAT or something else) in for the file name, I am trying to grab the extension to use it later on in my code to avoid any hard coding that may need changing later.

Below is just a sample of the code I currently have in place:
Code:
 for f in $2.DAT*
  do
    if [ -s $f ]; then
      scp -p $f $3/$2.DAT
    echo "Banner Admin job runs here"
    sleep 60

    rm $3/$2.DAT


$2 and $3 are parameters passed into the shell script.
$2's value would be one
$3's value is a target directory


Original File Names again:
one.dat
one.dat.002

Last edited by Franklin52; 03-05-2012 at 03:09 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 03-02-2012
Okay, that's what I thought. I should have left my original post there after all. Smilie

Anyway, given files in the current dir like this and assuming that the extension is after the first period:
Code:
$ ls
efs          one.dat      one.dat.002  one.txt      two
$

This code reads the current file list a file at a time then based on an Input Field Separator of "." parses the name into variables, which are then printed out. This should help you.
Code:
#!/bin/sh

ls|while IFS="." read filename extension the_rest
do
 echo "$filename \c"
 if [ -n "$extension" ]; then
   echo "[$extension] $the_rest"
 else
   echo "[no extension]"
 fi
done

exit 0

Output:
Code:
efs [no extension]
one [dat]
one [dat] 002
one [txt]
two [no extension]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying multiple files and appending time stamp before file extension

Hi, I have multiple files that read: Asa.txt Bad.txt Gnu.txt And I want to rename them using awk to Asa_ddmmyytt.txt and so on ... If there is a single command or more efficient executable please share! Thanks! (4 Replies)
Discussion started by: Jesshelle David
4 Replies

2. Shell Programming and Scripting

Split a file into multiple files with an extension

Hi I have a file with 100 million rows. I want to split them into 1000 subfiles and name them from 1.xls to 1000.xls.. Can I do it in awk? Thanks, (8 Replies)
Discussion started by: Diya123
8 Replies

3. UNIX for Dummies Questions & Answers

archieve and zip the multiple file with no extension

Hi, I'm new to scripting. I need to find all the 10 files in the source directory and then archieve them to archive directory. The source files which im getting does not have any extensions just binary files. I need to find them by the file names and archive it. Directory also contains other... (1 Reply)
Discussion started by: etldeveloper
1 Replies

4. Shell Programming and Scripting

removing a word in a multiple file starting at the dot extension

hi I would like to ask if someone knows a command or a script on how to rename a multiple file in the directory starting at the end of the filename or at the .extension( i would like to remove the last 11 character before the extension) for example Below is the result of my command ls inside... (5 Replies)
Discussion started by: jao_madn
5 Replies

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

6. UNIX for Advanced & Expert Users

get the file extension having multiple periods

How to get the file extension having multiple periods suppose i have a file samplewprk.txt.dat I need to retrieve txt.dat from the file. I worked all iam getting is either txt or pyd Could anyone help me in providing the solution? Thanks, in advance (3 Replies)
Discussion started by: chinku
3 Replies

7. UNIX for Dummies Questions & Answers

Removing prefix from multiple files and renaming file extension

Hello i have the files in this format pdb1i0t.ent pdb1lv7.ent pdb1pp6.ent pdb1tj2.ent pdb1xg2.ent pdb2b4b.ent pdb2ewe.ent Now i have to remove the prefix pdb from all the files and also i need to change the extension of .ent to .txt The new file should look like this ... (3 Replies)
Discussion started by: empyrean
3 Replies

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

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

10. UNIX for Dummies Questions & Answers

string search in folders with particular multiple file extension

Hi, I am newbie in UNIX so please excuse for my questions. Is there a a way to search for string in files within folder and sub folder in particluar file extensions. Ex. search for ABC in folder 'A'(including it's sub folders) in html, xml files. Thanks, Ani (2 Replies)
Discussion started by: anikanch
2 Replies
Login or Register to Ask a Question