The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding my DNS jjinno UNIX for Dummies Questions & Answers 2 07-17-2007 10:14 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM
Help finding a BOS zuessh AIX 1 07-25-2006 03:48 AM
Finding max value Enigma23 Shell Programming and Scripting 1 04-10-2005 09:34 AM
Finding how done IT geoquest UNIX for Dummies Questions & Answers 5 08-26-2002 11:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-21-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Finding last row

Using Linux and Bash, I have a script that outputs filenames with complete path, like this:
Quote:
/foo/bar/filename.pdf
/foo/bar/filename with space.pdf
/foo/filename.txt
I would like the following output:
Quote:
filename.pdf
filename with space.pdf
filename.txt
And I would like to get the filenames only. Tricky part is that I cannot predict how many levels deep the filename is located, so I cannot use standard Bash-kungfu to solve this...

Last edited by indo1144; 09-21-2005 at 10:20 AM..
  #2 (permalink)  
Old 09-21-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Add this line to the script


Code:
FILENAME=${file##*/}

where file would hold contain the values


Code:
/foo/bar/filename.pdf
/foo/bar/filename with space.pdf
/foo/filename.txt

one at a time.

And FILENAME would hold filename.pdf filename with space.. et al.

vino
  #3 (permalink)  
Old 09-21-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Quote:
Originally Posted by vino
Code:
FILENAME=${file##*/}
Now that worked perfectly!

I have no idea what this means, though. You set a new variable FILENAME that is $file with some magic applied. I do not understand the syntax and I am eager to learn more about this, since I find this very useful. Would you care to explain?
  #4 (permalink)  
Old 09-21-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Dare to start out with the man pages ?

Check out the section Parameter Expansion under man sh
and the section Parameters under man ksh

-vino
  #5 (permalink)  
Old 09-21-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Quote:
Originally Posted by vino
Dare to start out with the man pages ?

Check out the section Parameter Expansion under man sh
and the section Parameters under man ksh

-vino
Found it! I wanted to read more about this, but did not realize this was a shell built-in option, so did not think of man bash (in my case)...

Appreciate the help!
  #6 (permalink)  
Old 09-21-2005
indo1144's Avatar
indo1144 indo1144 is offline
Registered User
  
 

Join Date: Jun 2002
Location: Netherlands
Posts: 54
Quote:
Originally Posted by indo1144
Found it! I wanted to read more about this, but did not realize this was a shell built-in option, so did not think of man bash (in my case)...
I believe I understand what happened...
Quote:
${parameter##word}
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the
beginning of the value of parameter, then the result of the expansion is the expanded value of parameter
with the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case)
deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter
in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @
or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is
the resultant list.
So the ##-case expands to the longest matching pattern deletion. Since the parameter is *, it removes all matching patterns in the array, until it has no matches left. You read in the entire pathname as an array which has a delimiter "/" and the final result is the filename only.

Am I right?

This would mean that /foo/bar/filename.pdf would result in filename.pdf, but if the input would have a trailing slash like so: /foo/bar/files/ it would return... nothing!
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0