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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help in extracting data with command from file in C ahjiefreak High Level Programming 10 03-20-2008 11:26 PM
Extracting specific info from finger command franny UNIX for Dummies Questions & Answers 1 12-05-2007 01:13 PM
Extracting only file size of the ls -ltr command. kingofprussia UNIX for Dummies Questions & Answers 6 12-03-2007 03:14 AM
sed substitute / for \ : error "Function can not be parsed" candyflip2000 Shell Programming and Scripting 3 07-18-2006 04:02 AM
Extracting one column from a ps -ef command m223464 Shell Programming and Scripting 6 04-03-2005 12:36 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 02-06-2009
phudgens phudgens is offline
Registered User
  
 

Join Date: Feb 2009
Location: Denver
Posts: 40
Extracting last parsed segment with the cut command

I can see in the man pages how cut can be used to extract a specified string by using the -f qualifier. However, I can't find anywhere how to specify the "last" parsed segment with the cut command. If the string you are parsing is (or can be) differant each time the script is run, but you know that you always want the last segment parsed from the input string, how do you so specify in your command?

THanks
Phudgens
Denver
  #2 (permalink)  
Old 02-06-2009
Gee-Money Gee-Money is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 48
instead of cut, you could use awk:

Code:
echo "one two three four" | awk '{print $NF}'
gives your "four", and:

Code:
echo "one two three" | awk '{print $NF}'
gives you "three".

The variable 'NF' by itself is the number of fields in the record (line), and if you prefix that with a '$' then that will give you the last field.

hth
  #3 (permalink)  
Old 02-09-2009
phudgens phudgens is offline
Registered User
  
 

Join Date: Feb 2009
Location: Denver
Posts: 40
Thanks for the help. My final code ended up being:

set CurrentPwd = `pwd`
set CurrentFolder = `echo $CurrentPwd | awk -F'/' '{print $NF}'`


Paul H.
  #4 (permalink)  
Old 02-09-2009
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,432
Quote:
Originally Posted by phudgens View Post
Thanks for the help. My final code ended up being:

set CurrentPwd = `pwd`
set CurrentFolder = `echo $CurrentPwd | awk -F'/' '{print $NF}'`


Paul H.
What about parameter expansion:
Code:
set CurrentFolder = ${PWD##*/}
  #5 (permalink)  
Old 02-09-2009
phudgens phudgens is offline
Registered User
  
 

Join Date: Feb 2009
Location: Denver
Posts: 40
Couldn't get that to work with either upper or lower case "pwd". Got a "Variable syntax" error message.
Closed Thread

Bookmarks

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 11:17 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