LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-13-2008, 04:26 AM   #1
azeus
LQ Newbie
 
Registered: Jan 2008
Posts: 14

Rep: Reputation: 0
Unhappy 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?
 
Old 03-13-2008, 04:38 AM   #2
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
my_path=root\/test
find `pwd` | sed -e 's/^.*'"$my_path"'/./'
 
Old 03-13-2008, 10:39 PM   #3
azeus
LQ Newbie
 
Registered: Jan 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by jschiwal View Post
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?
 
Old 03-13-2008, 10:54 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 13.37
Posts: 4,021

Rep: Reputation: 125Reputation: 125
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.
 
Old 03-14-2008, 01:21 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Code:
find `pwd` | awk '{gsub("root/test",".") }1'
 
Old 03-14-2008, 01:29 AM   #6
azeus
LQ Newbie
 
Registered: Jan 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by gilead View Post
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?
 
Old 03-14-2008, 01:42 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware, LFS
Posts: 1,526
Blog Entries: 5

Rep: Reputation: 98
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}@.@"
 
Old 03-14-2008, 02:24 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
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.

Last edited by chrism01; 03-15-2008 at 08:21 PM.
 
Old 03-14-2008, 05:23 AM   #9
azeus
LQ Newbie
 
Registered: Jan 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you everyone
The problem is solved.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full Path Verbal Kint Linux - General 7 01-30-2008 09:34 PM
how to remove path using sed dtcs Programming 2 12-25-2006 04:29 PM
Sed gives output in pipes only when buffer is full jahvascriptmaniac Linux - Software 2 08-31-2006 05:57 PM
Run any Script without metioning the full path shipon_97 Linux - Newbie 2 08-08-2006 10:49 PM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM


All times are GMT -5. The time now is 03:33 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration