Reading extension of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading extension of files
# 8  
Old 06-05-2009
Code:
echo "file_name.sh" |  sed 's/.*\.\(.*\)/\1/'

will work for all.
# 9  
Old 06-05-2009
it is just a regex.
first of all i m modifying the solution.
pl refer to this

Quote:
echo "myfile.ext.ext" | sed 's/.*\.\([^.]*$\).*/\1/p'
it is searching for the pattern like this..

.* - anything
\. - literal dot.
[^.]*$ - anything after dot except dot at the end.
\(..\) - save the regex found
\1 - replace the regexp saved.

in previous solution i was first searching for the pattern.
# 10  
Old 06-05-2009
Quote:
Originally Posted by panyam
Code:
echo "file_name.sh" |  sed 's/.*\.\(.*\)/\1/'

will work for all.

Why do you use an external command when you can do it in the shell?

Code:
filename=file_name.sh
extension=${filename##*.}

# 11  
Old 06-05-2009
Code:
Why do you use an external command when you can do it in the shell?

since i am not good in bash programming. Have to learn a lot SmilieSmilie
# 12  
Old 06-05-2009
cfajohnson is right! You can even do it with tcsh:
Code:
set filename = file_name.xyz
echo $filename:e

# 13  
Old 06-05-2009
Code:
ls | sed 's/.*\.//'

# 14  
Old 06-05-2009
Quote:
Originally Posted by panyam
since i am not good in bash programming. Have to learn a lot

It's not just bash; it's the standard Unix (a.k.a POSIX) shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Python - glob () - How to grep same files with different extension files

Hi I Have a directory and i have some files below abc.txt abc.gif gtee.txt ghod.pid umni.log unmi.tar How can use glob function to grep abc files , i have created a variable "text" and i assigned value as "abc", please suggest me how can we use glob.glob( ) to get the output as below... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

3. Shell Programming and Scripting

Replacing extension of files

hi, i am having a certain files in a folder where i need to replace the extension with similar file name for eg 1.csv 2.csv 3.csv 4.csv i need to replace the extension 1.csv 1.txt 2.csv 2.txt 3.csv 3.txt (3 Replies)
Discussion started by: rohit_shinez
3 Replies

4. Shell Programming and Scripting

Moving files extension one by one

Hi, I'm trying to do a script that move .Gz extension (one by one) in a new repertory called old-logs and then copy what's inside old-logs to a new.log but adding a date like this (something. gz: 2012:12:09). thanks in advance for your answers. (5 Replies)
Discussion started by: Froob
5 Replies

5. UNIX for Dummies Questions & Answers

How to list files with no extension together with *.prog files?

Hi, I know that to list files with no extension, we can use.. ls -1 | grep -v "\." And to list .prog files, we can use.. ls -1 *.prog or ls -1 | grep '.prog$' (4 Replies)
Discussion started by: adshocker
4 Replies

6. Shell Programming and Scripting

gzip the files with particular extension

Is there any way to compress only the files with .xml extension within a folder which in turn has many sub folders? gzip -r9 path/name/*.xml is not working This compression is done in the Windows server using Batch script. (2 Replies)
Discussion started by: Codesearcher
2 Replies

7. UNIX for Dummies Questions & Answers

Checking files extension

Hi, I need one line command to display all files that ends with .scr. Example: In a directory I have 10 files, out of that 4 files have filetype extension .dat and 4 files with .scr and 2 files with .txt.... In this i want to display only files that ends with .scr. I tried some commands,... (2 Replies)
Discussion started by: gwgreen1
2 Replies

8. HP-UX

Files without extension

I am brand new to hp unix systems. I see some files without extension on this system. If I type name of the file it shows me so many detail but does not take me back to command prompt. What are these files and how do I come back to command prompt? Please help (1 Reply)
Discussion started by: rajahindustani
1 Replies

9. Shell Programming and Scripting

Reading filenames with extension .xml

Hi, I want to write a script to read all the filenames with extension .xml in a directory and pass the name of the file, one by one, to another function. Please help me out. Regards. Saurabh (3 Replies)
Discussion started by: bhalotias
3 Replies

10. UNIX for Dummies Questions & Answers

How to test for files with a particular extension.

Hi!!, Is there any way on Ksh to test the files having a particular Extension? My program looks something like: for i in *$1* do if "$i" != files with ext of .Z then compress -f $i fi done Any suggestions??? :):) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question