LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-02-2011, 06:31 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
replace string with empty string of dir path


Hi, I want to replace a string of directory path in a string to empty:
Code:
dd="test"
STRING="/mnt/sda1/record/$dd/"
j=$(echo "/mnt/sda1/record/test/somefile.test"|sed 's/$STRING//g')
But this doesnt seem to give me the desired thing:
Code:
j=$(echo "/mnt/sda1/record/test/somefile.test"|sed 's/\/mnt\/sda1\/record\/test\///g')
this gives the desired outcome, but its specific, i need a variable in the sed not a string. And if I replace STRING="\/mnt\/sda1\/record\/$dd\/" then I cant use it for something else, cause its has all the weird backslashes now.
Thanks
 
Old 04-02-2011, 06:38 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
If the pattern is a variable you have to let the shell substitute it by changing single quotes with double quotes, otherwise the word $STRING is interpreted literally. If the pattern contains slashes, you might use another character as separator in the s command, e.g.
Code:
... | sed "s:$STRING::g"
Also check the basename command, since from your example it provides the required functionality.

Last edited by colucix; 04-02-2011 at 06:39 AM.
 
1 members found this post helpful.
Old 04-02-2011, 07:37 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or you could store the path and use parameter substitution:
Code:
dd="test"
STRING="/mnt/sda1/record/$dd/"
path="/mnt/sda1/record/test/somefile.test"

j=${path#$STRING}
 
1 members found this post helpful.
Old 04-02-2011, 07:43 AM   #4
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Original Poster
Rep: Reputation: 2
thanks!
 
  


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] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 08:22 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
replace a string in a file using a script with path containig / vimalraj Linux - Newbie 3 02-27-2010 01:15 AM
Shell scripting - Getting just directory string from file path string? arashi256 Programming 5 10-16-2009 09:21 AM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-05-2008 12:59 AM


All times are GMT -5. The time now is 10:30 PM.

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