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
Replacing a string in nth line maxmave Shell Programming and Scripting 1 06-04-2008 02:32 PM
regexp to get first line of string jonas.gabriel Shell Programming and Scripting 3 05-09-2007 12:46 AM
get the NF of a string line ozzman Shell Programming and Scripting 5 02-01-2007 09:01 PM
How I Can get String From the Line habuzahra UNIX for Dummies Questions & Answers 2 10-29-2006 03:55 AM
Add string after line X alienET Shell Programming and Scripting 2 10-13-2005 06:52 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-19-2007
Registered User
 

Join Date: Dec 2007
Posts: 3
To cut end string from line

HI,

I want to cut end string from line.

e.g. i have following input line

/users/home/test.txt

I want to get end string 'test.txt' from above line and length of that end string will change and it always start after '/'.

Thanks,

Visu
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-19-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 674
Post

Code:
# basename /users/home/test.txt
test.txt
# dirname /users/home/test.txt
/users/home
Reply With Quote
  #3 (permalink)  
Old 12-19-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,445
Code:
# s="/users/home/test.txt"
# echo ${s##*/}
test.txt
Reply With Quote
  #4 (permalink)  
Old 12-19-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 674
Quote:
Originally Posted by ghostdog74 View Post
# echo ${s##*/}
Not bourne safe

Last edited by Smiling Dragon; 12-19-2007 at 06:42 PM. Reason: Added emote - don't want it to sound like something it's not
Reply With Quote
  #5 (permalink)  
Old 12-19-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,445
Quote:
Originally Posted by Smiling Dragon View Post
Not bourne safe
thanks.just curious, do you still use the bourne shell today?
Reply With Quote
  #6 (permalink)  
Old 12-19-2007
Registered User
 

Join Date: Jun 2007
Posts: 355
awk

Hi,

input:
Code:
/users/home/test.txt
/users/home/bin/test_test.txt
/users/local/home/testaa.txt
output:
Code:
test.txt
test_test.txt
testaa.txt
code:
Code:
awk 'BEGIN{FS="/"}
{
print $NF
}' filename
Reply With Quote
  #7 (permalink)  
Old 12-20-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 674
Quote:
Originally Posted by ghostdog74 View Post
thanks.just curious, do you still use the bourne shell today?
Of course!
if you're writing shell scripts for system stuff or if you are working as root, bourne is the only safe shell to use IMO.
For simple userland stuff I'll use bash, but even then I still script in sh - the extra features of ksh are nice but I only use ksh script when I need them as sh scripts work in everything else.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:29 PM.


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