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
Read the directory path sollins Shell Programming and Scripting 1 11-05-2008 05:09 AM
Perl script variable to read shell command bulkbiz Shell Programming and Scripting 4 10-10-2008 03:05 AM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 03:24 PM
read a path bkan77 Shell Programming and Scripting 4 07-18-2007 03:37 PM
read a path bkan77 SUN Solaris 2 07-18-2007 01:23 PM

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 12-05-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
need sed command to read a path and set to variable

I have a variable called PATH that contains a path
example: /Users/rtipton/Desktop/testusers/test

I need a sed command to set a variable called USER to the last directory name in that path

PATH="/Users/rtipton/Desktop/testusers/test"

and from that PATH i need USER to = test

I know sed can do this but sed usage is still a bit of a mystery to me

Thanks,
Rob
  #2 (permalink)  
Old 12-05-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669

Code:
# USER=`echo "/Users/rtipton/Desktop/testusers/test" | sed 's/.*\/\(.*\)/\1/'`;echo $USER
test

  #3 (permalink)  
Old 12-05-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
Quote:
Originally Posted by Ikon View Post
Code:
# USER=`echo "/Users/rtipton/Desktop/testusers/test" | sed 's/.*\/\(.*\)/\1/'`;echo $USER
test
Very nice, works well. Can you do me a huge favor and break down the sed command you used here and why it works the way it does... I am still trying to figure out how to properly use it and I learn by examples better than reading hypothetical explanations.

Thanks,
Rob
  #4 (permalink)  
Old 12-05-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
's/ --- Substitute directive
.*/ --- contains everything upto last / ("/Users/rtipton/Desktop/testusers/")
(.*) --- contains everything after last / (test)
/\1/ --- what to substitute (like $1 variable, what is inside the ( ) is stored in \1 )

there are extra \ because you have to escape metacharacters

I didnt know much about sed and awk before I found this site. The way I am learning, is people ask how to do something, I read others solutions and try to replicate it.

I have created my own wiki to keep all my notes in.

here is one site I found helpful:

http://student.northpark.edu/pemente/sed/sed1line.txt
  #5 (permalink)  
Old 12-05-2008
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Must it be sed??


Code:
MYPATH="/Users/rtipton/Desktop/testusers/test"
USER=`basename $MYPATH`

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 09:07 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