LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
  Search this Thread
Old 12-08-2016, 03:04 AM   #1
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Rep: Reputation: 0
sed: -e expression #1, char 57: unterminated `s' command


Hi Everyone,

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?

Thanks
 
Old 12-08-2016, 03:06 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,516
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
It looks like you forgot to escape one of the double quotes in your script.
 
Old 12-08-2016, 03:16 AM   #3
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
@Turbocapitalist sorry I didn't get on which double quotes you talking about.

Thanks
 
Old 12-08-2016, 03:23 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,516
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
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.
 
Old 12-08-2016, 04:10 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,516
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
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.
 
Old 12-08-2016, 07:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
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?
 
Old 12-08-2016, 11:31 PM   #7
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
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

Code:
"subject" : "This HTTP alert on $device.node_id"
Thanks
 
Old 12-09-2016, 02:53 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,516
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
You might benefit from temporarily putting set -x as the second line in your script. That will show you what the system really will use in each line.
 
Old 12-09-2016, 09:00 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,535

Rep: Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077
Quote:
Originally Posted by TashiDuks View Post
Code:
             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.
Code:
newSub2="$(sed 's@/@\\/@g' <<<$newSub)"
sudo sed -e 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1'"$newSub2"' \2/' .mymy.conf
There would still be a problem with backslash characters, but those are likely to be absorbed by the read command anyway.
 
Old 12-13-2016, 11:33 PM   #10
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
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.
Code:
newSub2="$(sed 's@/@\\/@g' <<<$newSub)"
sudo sed -e 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1'"$newSub2"' \2/' .mymy.conf
There would still be a problem with backslash characters, but those are likely to be absorbed by the read command anyway.
Hi,

Thanks for the help. I have finally got workable script using following line:
Code:
 sudo sed -i 's/\("subject" : "\)[^<]*\(\$device.node_id"\)/\1'"$newSub"' \2/' .mymy.conf
Cheers
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting error "/bin/sed: -e expression #1, char 13: extra characters after command" novicunix Programming 11 11-04-2014 01:35 PM
[SOLVED] sed "expression #1, char 2: extra characters after command" TheBigMing Linux - Newbie 9 03-26-2013 12:19 PM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed unterminated `s' command random0munky Linux - Newbie 7 08-19-2011 11:43 AM
sed: Unterminated 's' command whatis Programming 2 11-02-2009 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 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