The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parse String Using Sed racbern Shell Programming and Scripting 4 04-23-2008 09:14 AM
Parse String from a Variable racbern Shell Programming and Scripting 3 04-07-2008 09:04 AM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 01:43 AM
String parse question mnreferee Shell Programming and Scripting 5 03-06-2007 08:30 PM
parse variable mpang_ Shell Programming and Scripting 2 01-03-2007 03:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2005
Registered User
 

Join Date: Jan 2002
Posts: 18
Stumble this Post!
parse a string variable

Hello all, need a little help.

I have an input variable such as ARGV[0] which equals something like

/use/home/name/script/test.dat

I need to be able to get just the "test.dat" (i.e. the file name) at the end of the directory and the directory can be anything and any length. To put it another way, I want the value after the last "/".

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-18-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,837
Stumble this Post!
Are you in shell? If so basename will return the name of the file minus the path.

The arguments to shell are referenced by $1 $2 $3 .....

In C there is a function char *basename(char *path_and_file)
Reply With Quote
  #3 (permalink)  
Old 10-18-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,837
Stumble this Post!
It just occurred to me that you must be in a perl script.... try something along these lines
Code:
use Shell qw(basename);
..........
........
$base=basename($file)
Reply With Quote
  #4 (permalink)  
Old 10-18-2005
Registered User
 

Join Date: Jan 2002
Posts: 18
Stumble this Post!
basename worked great! Thanks a ton. After gave me the direction (ie answer) I found that basename is both a PERL and a Unix command.

Thanks again for the quick response.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0