LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-24-2020, 06:53 PM   #1
redd9
Member
 
Registered: Nov 2013
Location: Canada
Distribution: RHEL, Ubuntu
Posts: 212
Blog Entries: 1

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


Hi everyone, I am very new to using sed and Bash scripting, and created this file to practise:
Code:
#!/bin/bash
readarray -d "|" myarray < listOfURLs
for i in "${myarray[@]}"
do
        #echo "{$i}" 
        sed -i "s@URL@$$i@g" HTMLTemplate > filledInHTMLTemplate
done
The purpose of the code is to read a bunch of URLs from listOfURLs, and replace the text URL in HTMLTemplate with one of the URLs ( a different one each time, iterating through the whole array).

However, I get the error
Code:
sed: -e expression #1, char 137: unterminated `s' command
when I try to run my script. I've tried escaping the sed line in different ways, but nothing helps. Any ideas?

Edit: Also, the HTMLTemplate contains the first URL over and over again replacing the text 'URL'.

Last edited by redd9; 03-24-2020 at 07:11 PM.
 
Old 03-24-2020, 09:40 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I am not sure if the double dollar in $$i has a purpose or is a typo.

Why did you comment out the echo? Most likely something in the $i (or $$i) causes sed's hiccup, and the echo is a good way to figure this out. Perhaps there is an "@" in the variable?
By the way, you probably intended echo "${i}", though echo "{$i}" shouldn't cause any harm.

Another oddity, though probably not related to the error message, is the usage of sed's -i option. Its effect is to modify the HTMLTemplate file and to output nothing. This is probably not your intention.

Last edited by berndbausch; 03-24-2020 at 09:41 PM. Reason: wording
 
2 members found this post helpful.
Old 03-24-2020, 09:42 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
First, you are misusing the "-i" option, which is telling sed to modify the HTMLTemplate file in place rather than sending a modified version to stdout. It's probably good that it didn't work.

To answer your specific question, the most likely cause is that $i is expanding to a string that contains one or more "@" characters. It's difficult to make the sed 's' command work reliably with arbitrary strings expanded into the command. Frequently, I use the ctrl-G (ASCII BEL, or '\007') as a delimiter in such situations, but to be completely general you might have to pre-scan the string and find some character that is not present there. How to insert such nonprinting characters into your script depends on which editor you are using.

[EDIT] I was a bit slow, I see.
 
3 members found this post helpful.
Old 03-24-2020, 10:28 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by rknichols View Post
[EDIT] I was a bit slow, I see.
Not quite. The problem with variable strings in sed substitutions had not occurred to me for some reason. Good insight.
 
Old 03-31-2020, 05:56 PM   #5
redd9
Member
 
Registered: Nov 2013
Location: Canada
Distribution: RHEL, Ubuntu
Posts: 212

Original Poster
Blog Entries: 1

Rep: Reputation: 82
Thank you everyone! I had mistyped the original script, so will mark this as solved.
 
Old 04-02-2020, 10:13 PM   #6
redd9
Member
 
Registered: Nov 2013
Location: Canada
Distribution: RHEL, Ubuntu
Posts: 212

Original Poster
Blog Entries: 1

Rep: Reputation: 82
For those interested, here is the script that actually worked:

Code:
#!/bin/bash
readarray -d "|" urls < parsedURLs

        for i in ${urls[@]}
        do
                sed "s,URL,${i},g" template >> filledTemplate

        done
I then ran

Code:
sort filledTemplate | uniq -u >> completeTemplate
to ensure there were no duplicates.

Last edited by redd9; 04-03-2020 at 11:32 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] jhalfs sed: -e expression #1, char 55:Invalid preceding regular expression percy_vere_uk Linux From Scratch 10 07-22-2017 07:15 AM
[SOLVED] sed: -e expression #1, char 57: unterminated `s' command TashiDuks Programming 9 12-13-2016 11:33 PM
SED command failing while substituting variable with error unterminated `s' command vnarvankar Linux - Software 2 03-10-2016 09:10 AM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
sed: Unterminated 's' command whatis Programming 2 11-02-2009 02:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:40 PM.

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