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


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-28-2012
Registered User
 
Join Date: Feb 2012
Posts: 18
Thanks: 25
Thanked 0 Times in 0 Posts
how to cut all string after the last delimiter?

hi all,

suppose a string:


Code:
abc/def/ghi/jkl/mn.txt

and i want to get the file name without the path.

however, different files have different paths, therefore the number of delimiter is uncertain.

thanks so much!
Sponsored Links
    #2  
Old 02-28-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Location: Out running a Marathon.
Posts: 2,192
Thanks: 48
Thanked 127 Times in 119 Posts
What about something like?


Code:
$ echo "abc/def/gh/joe.txt" | awk -F"/" '{print $NF}'
joe.txt

The Following 2 Users Say Thank You to joeyg For This Useful Post:
lan123 (03-06-2012), sunnydanniel (02-28-2012)
Sponsored Links
    #3  
Old 02-28-2012
Registered User
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Try using basename <full_path_to_file>

$ basename /sndc002/app/appsndc/sndcappl/admin/sndc01.txt
sndc01.txt
The Following User Says Thank You to bab@faa For This Useful Post:
sunnydanniel (03-01-2012)
    #4  
Old 02-28-2012
Registered User
 
Join Date: Jun 2008
Location: Singapore
Posts: 191
Thanks: 3
Thanked 45 Times in 44 Posts
If you are doing this within a loop, you may want to consider using the built-in feature of bash/ksh parameter expansion. This will save you 1 exec for every loop

Code:
v="abc/def/ghi/jkl/mn.txt"
echo ${v##*/}

The Following User Says Thank You to chihung For This Useful Post:
sunnydanniel (03-01-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to split a string with no delimiter saint34 Shell Programming and Scripting 8 03-01-2011 02:27 PM
Delimiter count in a string. priyam Shell Programming and Scripting 8 10-20-2008 11:16 AM
Extraction of string from Stringlist using delimiter spkandy Shell Programming and Scripting 3 10-03-2008 09:38 AM
Parsing string using specific delimiter primp Shell Programming and Scripting 8 09-22-2008 01:46 AM
creating a delimiter string satish@123 Shell Programming and Scripting 0 05-21-2008 05:38 AM



All times are GMT -4. The time now is 07:53 PM.