Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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'"
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.....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.