- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- extention of filename
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2008 04:03 AM
тАО07-31-2008 04:03 AM
Got stuck up with simple functionality. Just need to get the filename extention. I am not sure about the number of dots in the filename. It will be like,
filename.xls.zip
filename.xls
filename.in.xls.zip
just need to get the extention also. help me out. thx
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2008 04:13 AM
тАО07-31-2008 04:13 AM
Re: extention of filename
# F=filename.in.xls.zip
# basename ${F} .zip
filename.in.xls
See the manpages for 'basename'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2008 04:25 AM
тАО07-31-2008 04:25 AM
Re: extention of filename
What you have given is for stripping out the extention .zip.
But my requirement is to get the extention, here are some of the cases,
1) filename.xls.zip
-- I need to get zip as extention in a variable
2) filename.xls
-- I need to get xls as extention in a variable.
Thanks
Mayil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2008 04:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2008 04:39 AM
тАО07-31-2008 04:39 AM
Re: extention of filename
In the man page, look for ##
man -sh-posix
/##
Assuming a file 'x' with your list:
Usage example:
$ cat x.sh
for file in $(
ext=${file##*.}
echo --- $ext $file ---
done
$ ./x.sh
--- zip filename.xls.zip ---
--- xls filename.xls ---
--- zip filename.in.xls.zip ---
Using perl...
$ perl -lpe 's/.*\.//' x
zip
xls
zip
Cheers,
Hein.
Example of more file name matching variants.
$ cat x.sh
for file in $(
a=${file%.*}
b=${file%%.*}
c=${file#*.}
d=${file##*.}
echo --- $file $a $b $c $d ---
done
$
$ ./x.sh
--- filename.xls.zip filename.xls filename xls.zip zip ---
--- filename.xls filename filename xls xls ---
--- filename.in.xls.zip filename.in.xls filename in.xls.zip zip ---
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2008 03:50 AM
тАО08-02-2008 03:50 AM
Re: extention of filename
To make it more simple:
ll file*|awk -F\. '{print $NF}'
ll filename*|awk -F"." '{print $NF}'
-F with awk is the delimiter (here . - that is escaped with either a backslash or quotes) which splits the columns and $NF is the last field. So you do not have to worry about the number of dots.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2008 04:41 AM
тАО08-02-2008 04:41 AM
Re: extention of filename
put the commands in between the back quotes (`) the leftmost key in the normal PC keyboard (with the tilde ~).
for i in `ll file*|awk -F\. '{print $NF}'`
do
echo $i
done
zip
xls
zip
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2008 03:14 PM
тАО08-02-2008 03:14 PM
Re: extention of filename
Rasheed>> To make it more simple:
How is that any more simple than what Ivan already showed.
Rasheed>> put the commands in between the back quotes (`)
Hmmm, you may want to consider using $() as shown in an earlier reply here. It seems to be the way forward, and IMHO is more readable.
Also.... if you just want the file names, then why make the system pick up all attributes. That is, just use ls instead of ll for this excercise.
But really... why activate a sub-process to run ls at all? These are simple, shell-only, tasks. My earlier example assmed a list of file names as input.
If there is a directory with file as per your example, then you can use for example:
$ ls file*
filename.in.xls.zip filename.xls filename.xls.zip
$
$ for file in *(file*)
> do
> echo ${file##*.}
> done
zip
xls
zip
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2008 01:32 AM
тАО08-03-2008 01:32 AM
Re: extention of filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-04-2008 06:20 PM
тАО08-04-2008 06:20 PM
Re: extention of filename
Hmm, this works, but it completely unexpected, if you read the documentation closely.
awk(1) says that -F is a regular expression. And later it implies it only does extended regular expressions. And a "." is special. So you should have to use:
-F'\.'
There was another thread using "||" and both had to be quoted but a single one doesn't???
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1253990
So this seems to imply that if -F or FS only has one char, it stands for itself. ???
>Hein: for file in *(file*); do
Any reason you used composite patterns rather than the obvious:
for file in file*; do
You aren't getting the zero or more part.
I.e. there must be some other rule that says that if there are no other matches, then that zero matches isn't selected.