ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 have configuration file named "mymy.conf" with following content:
Code:
"subject" : "This HTTP alert on $device.node_id"
I want to replace text This HTTP alert on to something else as per the user input.
I have come up with following script:
Code:
#!/bin/bash
read -p "Do you want to change mail subject? [y/N]" responseMailSub
case "$responseMailSub" in
y|Y )
#Ask for new mail subject
echo "Enter New Mail Subject: "
read newSub
#Update the file
sudo sed -e 's/\(\"subject" : "\)[^<]*\(\$device.node_id"\)/\1'$newSub' \2/g' .mymy.conf
echo "Your mail subject changed to $newSub"
;;
n|N )
echo "Operation canceled... ";;
* ) echo "Wrong option used. Operation terminated!!!"
;;
esac;;
After running above script i get sed: -e expression #1, char 57: unterminated `s' command message.
To check the syntax i tried running same directly on TERMINAL sed -e 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1This FTP alert on \2/g' .mymy.conf and it worked fine.
Can anyone tell me why there is difference? Any syntax correction?
In the sed options you have a lot of quotes. You've escaped one double quote but not the others. However, that probably is not the issue. You also have single quotes there around $newsub that need to be escaped or eliminated. Walk through the whole -e option and match each pair of quotes, start and end, and you'll see.
Also, the actual content of $newsub will affect how sed runs or fails, especially if it contains a slash or whatever delimiter you happen to use for the s command in sed. You may want to sanitize that input.
If the file mymy.conf contains a single line, why does the sed have to be so complicated? If it is the case that there are more lines in the file, what is unique about the line to be changed or do
we really need to regex the entire line because there is another in the file so similar that this is required?
As an example, if it were this single line, I would simply use:
Code:
sed "s/T.*on/$newSub/" mymy.conf
So please provide more details about the input file and what is required?
In the sed options you have a lot of quotes. You've escaped one double quote but not the others. However, that probably is not the issue. You also have single quotes there around $newsub that need to be escaped or eliminated. Walk through the whole -e option and match each pair of quotes, start and end, and you'll see.
The double quotes are for my following lines in my configuration file
sudo sed -e 's/\(\"subject" : "\)[^<]*\(\$device.node_id"\)/\1'$newSub' \2/g' .mymy.conf
You insert $newsub unquoted. If that string contains any spaces or other field separator characters, it's going to end the sed argument prematurely. And, note that escaping that double quote that precedes "subject" is unnecessary, and there is little point to the "g" modifier since there should never be more than one occurrence of the pattern. Try:
Code:
sudo sed -e 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1'"$newSub"' \2/' .mymy.conf
There will still be a problem if $newsub contains any "/" characters, since those would corrupt the substitution command. You might want to pre-process $newsub to escape any "/" characters.
You insert $newsub unquoted. If that string contains any spaces or other field separator characters, it's going to end the sed argument prematurely. And, note that escaping that double quote that precedes "subject" is unnecessary, and there is little point to the "g" modifier since there should never be more than one occurrence of the pattern. Try:
Code:
sudo sed -e 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1'"$newSub"' \2/' .mymy.conf
There will still be a problem if $newsub contains any "/" characters, since those would corrupt the substitution command. You might want to pre-process $newsub to escape any "/" characters.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.