LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-23-2012, 05:15 PM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
replace path


hi guys
i have a simple question

i have some variables they contain path addresses i want to replace old address with new address

e.g

echo $m=/tmp/files3/New/test

i want to replace /tmp/files3 with /root/new

i used sed but it didnt work

echo $m | sed "s-/tmp/files3-/root/new"

what ca i do ???
 
Old 10-23-2012, 05:53 PM   #2
jack_
Member
 
Registered: Oct 2012
Location: /
Distribution: Slackware, Debian, Ubuntu, CentOS, OpenSuse
Posts: 35

Rep: Reputation: Disabled
export m='/tmp/files3'
 
Old 10-23-2012, 05:55 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Where do you want to replace them? In a script, your environment just for this session, in your environment permanently? Where are the original values stored?
 
Old 10-23-2012, 06:00 PM   #4
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jack_ View Post
export m='/tmp/files3'
what's this ???
???????????!!!!!!!!
 
Old 10-23-2012, 06:10 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by mmhs View Post
???????????!!!!!!!!
What is that?
 
Old 10-23-2012, 06:13 PM   #6
jack_
Member
 
Registered: Oct 2012
Location: /
Distribution: Slackware, Debian, Ubuntu, CentOS, OpenSuse
Posts: 35

Rep: Reputation: Disabled
Quote:
Originally Posted by mmhs View Post
what's this ???
???????????!!!!!!!!
whoop my bad, i don't know what i was thinking.

here:

echo $m | sed 's/tmp\/files3/root\/new/'
 
Old 10-23-2012, 06:18 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by jack_ View Post
whoop my bad, i don't know what i was thinking.

here:

echo $m | sed 's/tmp\/files3/root\/new/'
Well, if anything, it'll be:

Code:
echo $m | sed 's/\/tmp\/files/\/root\/new/'
but without knowing the context, I still fail to see the point of doing all of that.
 
Old 10-25-2012, 01:48 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
There's no need for sed. Once a value is in a variable, you really only need built-in parameter substitution.

Code:
m=/tmp/files3/New/test
a=/tmp/files3
b=/root/new

echo "${m/$a/$b}"

echo "$b${m#$a}"
The second one is a more portable version (the "${var/match/replace}" pattern is only available in bash and some other modern shells), but it assumes that the string to replace is at the front of the variable.


Bash also has many more string manipulation options available, and you should generally rely on them for simple operations like this. Save the external tools like sed and awk for operating on text files and other large blocks of text that need to be processed en masse.

Last edited by David the H.; 10-25-2012 at 01:50 PM. Reason: missed word
 
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] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 07:43 AM
replace a string in a file using a script with path containig / vimalraj Linux - Newbie 3 02-27-2010 01:15 AM
how to replace path nasridine Linux - Newbie 10 02-17-2010 07:05 PM
sed attempting to replace text with directory path emgey Programming 5 04-03-2008 08:05 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 12:18 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