sed: -e expression #1, char 40: unterminated `s' command - unix

When I run the command
sed -i.backup "/proxySettings['nginxCacheSize']/s/$/ . " inactive=5h"/"
I get the error
sed: -e expression #1, char 40: unterminated `s' command
Any idea how to fix it?

First, let's leave the -i flag out of this for now, and simplify the command a little:
sed "/'foo']/s/$/ "bar"/"
You have both single quotes and double quotes inside your expression, and sed is getting confused about where the command begins and ends. As written, it appears to be this:
sed "/'foo']/s/$/ "
(with some gibberish after it), which clearly has an unterminated s command. We could use single quotes instead of double quotes to demark the command, but then we'd have the same problem with the single quotes around foof. So we escape those internal double quotes with backslashes:
sed "/'foo']/s/$/ \"bar\"/"

Related

Replacing Value (URL) in Properties file in Unix

I am trying to replace value of the property file in unix using Sed command. Here is the code:
Properties file:
Test.abc.homeURL:https://www.abdfghjc.net/instant-cash-offer/?from=1&LNX=TIMATCMOB&oCode=CMB
Shell:
MOBILE_HOME_PAGE_KEY="Test.abc.homeURL"
MOBILE_REPLACE_URL="http://ww.xyz.com/default.html"
sed -i
"s/^$MOBILE_HOME_PAGE_KEY:.*/$MOBILE_HOME_PAGE_KEY:$MOBILE_REPLACE_URL/"
propertiesFile
Error:
sed: -e expression #1, char 72: unknown option to `s'
Looks like some regular expression error, couldn't figure it out. please help me?
Thanks,
You have to use a different character than slash (/) to separate the sed parameters, because you have slashes in one of the variables ($MOBILE_REPLACE_URL).
Just use another character like #. You also couldn't use colon because you have colon in your variables.
As far as I understood , your requirement is to replace the key MOBILE_HOME_PAGE_KEY with MOBILE_REPLACE_URL key's value.
Below is what I tried
sed -e 's/^MOBILE_HOME_PAGE_KEY=.*/MOBILE_HOME_PAGE_KEY=\"http://ww.xyz.com/default.html\"/' newfile
newfile is the filename

Julia language: quotes in run() are not recognized? Pipeline error

Julia is ignoring quotes when I use run(), e.g.:
run(`cat file.txt | sed "s/blah/hi/"`)
ignores the quotes, which are required.
\"
does not work...
EDIT: The error is with the pipeline:
cat: |: No such file or directory
cat: sed: No such file or directory
cat: s/blah/hi/: No such file or directory
ERROR: failed process: Process(`cat file.txt | sed s/blah/hi/`, ProcessExited(1)) [1]
in pipeline_error at process.jl:502
in run at ./process.jl:479
The | does not create a pipe inside of Julia backtick syntax. Instead, you are invoking the cat program with four arguments:
file.txt
|
sed
s/blah/hi/
Since these files are unlikely to all exist, cat terminates with an error. Note that sed does not require quotes around the last argument. In fact, if it did get quotes then it wouldn't do what you want at all since the program would be a single string literal. It's the shell that sees the double quotes and passes their contents to sed as a single argument. In this case, since there are no spaces or other characters that are special to most shells between the quotes, it makes no difference. To accomplish what you want you can do this:
run(`cat file.txt` |> `sed "s/blah/hi/"`)
The double quotes are optional as they are in the shell since there are no spaces or other special characters inside of the argument to sed.

SED substitute from variable containing special characters

I want to use SED in order to substitute a placeholder (##ERROR_MSG##) contained in a .txt file with the content of a variable ($ERROR_MSG)
here the command I use:
cat FILE.TXT | sed "s/##ERROR_MSG##/$ERROR_MSG/"
here what is contained in $ERROR_MSG (contains special characters and is multi line)
ERROR:
ORA-12170: TNS:Connect timeout occurred
ERROR:
ORA-12162: TNS:net service name is incorrectly specified
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]] where <logon> ::= <username>[/<password>][#<connect_identifier>]
<proxy> ::= <proxyuser>[<username>][/<password>][#<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
I recieve the following error message:
sed: -e expression #1, char 22: unterminated `s' command
The only robust way to do this, which will work with any value of $ERROR_MSG is:
awk -v old="##ERROR_MSG##" -v new="$ERROR_MSG" 's=index($0,old){$0 = substr($0,1,s) new substr($0,s+length(old))} 1' file
try this:
sed "s+##ERROR_MSG##+$ERROR_MSG+" FILE.TXT
your line didn't work because the text in $ERROR_MSG contains slash /.
edit:
try this awk oneliner too:
awk -v t="$ERROR_MSG" '{sub(/##ERROR_MSG##/,t)}7' FILE.TXT

sed command in bash input and output file

Does anyone know what this means?
sed -e 's/\r$//' inputfile > outputfile
This is what I have so far:
\r refers to Carriage Return (CR)
so possibly Swap the blanks for Return Carriage? in the inputfile?
I'm not too sure really
It's changing files from CRLF-terminated lines into LF-terminated lines. The former tend to be Windows-type files where each line ends with a carriage-return/linefeed (CRLF or \r\n).
UNIX-type files just have a newline character (LF or \n).
Specifically, that sed command substitutes \r at the end of a line (indicated by $) with nothing, the same as s/xyzzy/plugh/ would change the first xyzzy in the line into plugh.
sed is the name of the program you call.
-e tells sed that the following argument is the expression to run.
s/\r$// is a substitution: it tells sed to replace carriage return at the end of line ($) with nothing. Sed does that for each line.
inputfile is the file from which sed reads its input.
> is a redirection operator, it means the output of sed will be redirected to outputfile.
Basically, the result should be the same as dos2unix (sometimes renamed to fromdos).

sed error: unterminated 's' command

I run below sed command
sed -i s/abc=.*$/abc=def ghi/g hpq_sf_attach_wf_param.txt
and it gave me error:
sed: -e expression #1, char 17: unterminated `s' command
I noticed it is due to space in between of def and ghi.
Any idea how to fix it?
You need to use quoting to protect special characters, including spaces, $, and *.
sed -i 's/abc=.*$/abc=def ghi/g' hpq_sf_attach_wf_param.txt
So geekosaur had it right. The the reason you had the problem though is because it needs to be double quotes for the wildcards because with single quotes it takes them as literal characters, not for the meaning you want.
sed -i "s/abc=.*$/abc=def ghi/g" hpq_sf_attach_wf_param.txt
Also if the space between "def" and "ghi" gives you problems, adding a "\" should help making it read it as a literal space.
sed -i "s/abc=.*$/abc=def\ ghi/g" hpq_sf_attach_wf_param.txt

Resources