LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-30-2007, 10:55 PM   #1
kamkos
LQ Newbie
 
Registered: Jan 2004
Posts: 16

Rep: Reputation: 0
shell scripting problem, sed command


i am working on shell scripting and i want to change a text inside a file. I know i have to use sed command but there is a problem. When i run the script i am passing three arguments

$ ./program arg1 arg2 arg3
arg1 = file path to the directories
arg2= the string that it needs to find
arg3=the new string that replaces arg2

Nevertheless, inside the program file i have a line
sed 's/'$2'/'$3'/g' $x>out.txt

and here is the problem. when i run the script it says:
sed: -e expression #1, char 0: no previous regular expression

I know it has something to do with the quotes ('s/'$2'/'$3'/g') because when i change the variables ($2, $3) to regular strings it does the job. I think i tried all kinds of combinations on how to get this line to work but with no luck.

can somebody tell me the correct combination of quotes?

thanks,
 
Old 03-30-2007, 11:45 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
I know it has something to do with the quotes ('s/'$2'/'$3'/g') because when i change the variables ($2, $3) to regular strings it does the job. I think i tried all kinds of combinations on how to get this line to work but with no luck.

can somebody tell me the correct combination of quotes?
Try this:
"s/$2/$3/g"

Two points:
I don't think you ever need to quote the value of a variable.
The single quote (') is called a strong quote and forces everything inside to be taken literally. Thus, your syntax caused sed to look for the literal string "$2"
OOPS--no: it was looking for "'$2'"

Last edited by pixellany; 03-30-2007 at 11:47 PM.
 
Old 03-31-2007, 11:11 AM   #3
kamkos
LQ Newbie
 
Registered: Jan 2004
Posts: 16

Original Poster
Rep: Reputation: 0
thanks pixellany but the problem persists. It still gives me the error.

sed: -e expression #1, char 0: no previous regular expression


any other ideas?

thanks,
 
Old 03-31-2007, 12:40 PM   #4
kamkos
LQ Newbie
 
Registered: Jan 2004
Posts: 16

Original Poster
Rep: Reputation: 0
finally !!!!!!

i have found the way:

's/'"$2"'/'"$3"'/g'


damn, what a pickle

thanks,
 
Old 03-31-2007, 09:31 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by kamkos
finally !!!!!!

i have found the way:

's/'"$2"'/'"$3"'/g'


damn, what a pickle

thanks,
This may have worked for this situation, but creating code that convoluted will eventually get you in trouble. I am not even going to TRY to understand how triply-nested quotes somehow make things work.
If I could, I would really want to dig into why the simple structure I tested on my machine would not work on yours.....
 
Old 03-31-2007, 09:39 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This also works:

s/$2/$3/g (NO quotes)
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 11:36 AM
shell command help, sed i think infamous41md Programming 2 08-15-2004 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:29 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
Open Source Consulting | Domain Registration