LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to use script sed with full path (https://www.linuxquestions.org/questions/programming-9/how-to-use-script-sed-with-full-path-627706/)

azeus 03-13-2008 04:26 AM

how to use script sed with full path
 
I have a script like that

find `pwd` | sed -e "s\^.*root\/test/./"

I get an error if I use a parameter replace the path like that

my_path=root/test
find `pwd` | sed -e "s\^.*$my_path/./"

what should I do?

jschiwal 03-13-2008 04:38 AM

my_path=root\/test
find `pwd` | sed -e 's/^.*'"$my_path"'/./'

azeus 03-13-2008 10:39 PM

Quote:

Originally Posted by jschiwal (Post 3087142)
my_path=root\/test
find `pwd` | sed -e 's/^.*'"$my_path"'/./'

It does not work.
error occur


if I take out the single quotation mark like that, no error occur.
find `pwd` | sed -e 's/^.*"$my_path"/./'
however, the sed will not replace the pre-fix "root/test" to "."


What is the different between single quotation mark and double quotation mark in sed?

gilead 03-13-2008 10:54 PM

It's the shell, not sed, that is parsing the quotes. Double quotes allow variable interpolation, single quotes don't. There's a fair bit of info in the bash man page about it (I'm assuming your shell is bash).

You could also use a different separator for the substitute command in sed - then you shouldn't need to quote the variable. I'm not at a box I can test that on at the moment though.

ghostdog74 03-14-2008 01:21 AM

Code:

find `pwd` | awk '{gsub("root/test",".") }1'

azeus 03-14-2008 01:29 AM

Quote:

Originally Posted by gilead (Post 3088141)
It's the shell, not sed, that is parsing the quotes. Double quotes allow variable interpolation, single quotes don't. There's a fair bit of info in the bash man page about it (I'm assuming your shell is bash).

You could also use a different separator for the substitute command in sed - then you shouldn't need to quote the variable. I'm not at a box I can test that on at the moment though.

thanks. yes, it is bash shell.
how do define another separator for the command in sed?

konsolebox 03-14-2008 01:42 AM

Hello gilead if it's ok i'll tell him for you. You can use the @ sign in sed if you like.

Code:

var="root/temp"
find `pwd` | sed -e "s@^.*${var}@.@"


chrism01 03-14-2008 02:24 AM

Punctuation marks are popular. For an alternative, I go with colon ':', seems to stand out nicely when reading, esp if you are sed'ing dir paths.

azeus 03-14-2008 05:23 AM

Thank you everyone
The problem is solved.


All times are GMT -5. The time now is 02:28 AM.