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
just want certail elements frenchface Shell Programming and Scripting 1 05-10-2008 06:38 PM
Map - printing all elements - why? dhanamurthy High Level Programming 0 04-14-2008 10:19 AM
To return the elements of array Sudhakar333 Shell Programming and Scripting 5 08-06-2007 11:20 AM
$PWD shows absolute path vs path w/symbolic links kornshellmaven Shell Programming and Scripting 3 06-13-2007 09:15 AM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 08:47 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-26-2008
Registered User
 

Join Date: Mar 2008
Posts: 1
Reomve elements from a path name

I have a srcipt that I pass a path into that will consist of 5 or 6 elements (/1/2/3/4/5/6). I want to always pull the third element out of the path name. How can i do this?

Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Code:
OLDIFS=$IFS; IFS=/; set -- $path; IFS=$OLDIFS
echo "$4"  # because the first element $1 will be empty

Last edited by era; 03-26-2008 at 11:38 AM. Reason: Save old IFS in OLDIFS
Reply With Quote
  #3  
Old 03-26-2008
redhead's Avatar
Registered User
 

Join Date: Feb 2002
Location: Denmark
Posts: 48
Quote:
Originally Posted by kgeasler View Post
I have a srcipt that I pass a path into that will consist of 5 or 6 elements (/1/2/3/4/5/6). I want to always pull the third element out of the path name. How can i do this?

Thanks
# echo "$1" | sed 's/\// /g' | awk '{print $3}'
Reply With Quote
  #4  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Actually, if by "pull out" you mean "remove" rather than "extract" (as per the topic title), do as above, then glue back together all except the one you don't want. Something like

Code:
p=
n=
s=
while true
do
  case $# in 0) break;; esac
  i=$1
  shift
  # skip $4
  n=x$n
  case $n in xxxx) continue;; esac
  p="$p$s$i"
  s=/
done
echo "$p"
That got messier than I thought, but it uses no external programs.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:21 AM.


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

Content Relevant URLs by vBSEO 3.2.0