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
extract a field by logic ChicagoBlues UNIX for Dummies Questions & Answers 4 01-27-2009 02:00 PM
need to extract field of characters in a line jansat Shell Programming and Scripting 3 05-22-2008 10:44 AM
How to extract field from variable more efective pp56825 Shell Programming and Scripting 11 01-19-2008 10:34 AM
Extract Part of string from 3rd field $3 using AWK stakuri Shell Programming and Scripting 4 10-10-2007 01:28 PM
Trying to extract a field from /etc/passwd file.. xBuRnTx UNIX for Dummies Questions & Answers 2 10-05-2005 03:45 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 03-11-2009
kcp_pavan kcp_pavan is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 3
how to extract last occurence of the field

path = /first/second/third/fourth. i want to extract /first/second/third from path.my program something like this ....

path=/first/second/third/fourth
noc=`echo $path|tr '/' '\n'|wc -w`
var='echo $noc|cut -d'/' -f 1-$noc --> is giving error.

why $noc is not working here.any other alternative to extract it.

Thanks
kcp_pavan
  #2 (permalink)  
Old 03-11-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,928

Code:
% path=/first/second/third/fourth 
% printf '%s\n' "${path%/*}"
/first/second/third
% printf '%s\n' "${path##*/}"
fourth

  #3 (permalink)  
Old 03-11-2009
Goldorakk's Avatar
Goldorakk Goldorakk is offline
Registered User
  
 

Join Date: Feb 2009
Location: France
Posts: 43
Talking

Quote:
Originally Posted by kcp_pavan View Post
path=/first/second/third/fourth
noc=`echo $path|tr '/' '\n'|wc -w`
var='echo $noc|cut -d'/' -f 1-$noc --> is giving error.

why $noc is not working here.any other alternative to extract it.
Hummmm ... you translate "/" in "\n" and after you want to "cut" with the "/" as delimiter ...

Try this :
Code:
$ path=/first/second/third/fourth
$ var=$(dirname $path)
$ echo $var
/first/second/third

  #4 (permalink)  
Old 03-12-2009
kcp_pavan kcp_pavan is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 3
reply

both soultions worked effectively. thanks to Goldorakk and radoulov .......

one more sol :
------------------
path=/first/second/third/fourth

echo $path | nawk '{print $NF}' FS="/" | read FILE
echo "File Name: "$FILE --> fourth

echo ${path%/*} | read PAT
echo "Path: "$PAT --> /first/second/third


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 12:06 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