LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2011, 02:46 PM   #1
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
Help with sed 'd


This probably does have a very simple answer, but i am unable to find it on google or anywhere else for that matter... i have a file that contains a filepath
Code:
 /Users/DuskFall
and i wish to remove all the slashs and the word Users from it, leaving just DuskFall so i can pull it back into a variable for the next part of my script. Thanks in advance.
p.s. I know grep would help here, except that filename changes as my script runs

Last edited by DuskFall; 08-11-2011 at 02:49 PM.
 
Old 08-11-2011, 03:04 PM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 533

Rep: Reputation: 79
Code:
user@Desktop:~$ echo "/Users/DuskFall" |sed -e 's/\/.*\/\([a-zA-Z]\)/\1/g'
DuskFall
user@Desktop:~$
 
1 members found this post helpful.
Old 08-11-2011, 03:06 PM   #3
bvm2607
LQ Newbie
 
Registered: Sep 2008
Posts: 6

Rep: Reputation: 2
echo "/Users/DuskFall" | sed 's/^.*\/\(.*\)$/\1/'

DuskFall
 
1 members found this post helpful.
Old 08-11-2011, 03:07 PM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 533

Rep: Reputation: 79
or even easyer with basename
Code:
user@Desktop:~$ basename /Users/DuskFall
DuskFall
user@Desktop:~$
 
Old 08-11-2011, 03:15 PM   #5
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you so much, i have it working now
 
Old 08-11-2011, 03:44 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,614

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

the sed's do not really need the backreferences here:
Code:
$ echo '/from/dusk/till/dawn' | sed 's@.*/@@'
dawn
Alternatively - if you are using bash - you can pull the path as is into your variable and extract the interesting part as follows:
Code:
$ var='/from/dusk/till/dawn'
$ echo ${var/*\//}
dawn
 
1 members found this post helpful.
Old 08-11-2011, 05:51 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by crts View Post
${var/*\//}[/CODE]
Or:

Code:
${var##*/}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration