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
Related
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\"/"
I'm trying to search in all files a text, and replace it with the word EXAMPLE. I do the following:
for f in /home/testu/zz*; do
sed -i "s/&VAR1\s*=\s*'?[1]{4}'?/EXAMPLE/g" "$f"
done
It gives no error, the files seems to be "updated" in the filesystem, but they wont get changed. If I test that regexp with the grep command it works fine, so something must be wrong with SED, could it be SED version?
Thanks in advance.
Your current sed command parses the regular expression as a POSIX BRE compliant pattern.
In BRE POSIX, ? matches a literal ? char, and { / } also match literal { / } chars. To make a range quantifier in a BRE POSIX pattern, you need to escape {...}, \{min,max\}.
The [1] is equal to 1, so the brackets are quite redundant here.
To fix your pattern, you may replace ? with \{0,1\} (0 or 1 occurrences) and {4} with \{4\}:
sed -i "s/&VAR1\s*=\s*'\{0,1\}1\{4\}'\{0,1\}/EXAMPLE/g" "$f"
Thanks to Wiktor Stribiżew tips, we got the solution (SSED GNU 4.1.5). The resulting regexp works with grep and sed. The code was a mix of solutions at the end.
sed -i "s/&VAR1\s*=\s*'\{0,\}1\{4\}'\{0,\};\{0,\}/EXAMPLE/g" "$f"
A few things:
Things like [[:blank:]] caused error of input file.
My sed version didnt support -E, so the {} had to be escaped, didn't know that :)
Thanks again Wiktor!
I'm trying to insert into a text file the string cd $var at the second line using sed, but it doesn't seem to work. I'm using the syntax for inserting a line at a specific line in a file,
sed -i '2icd $var' FILE
The format of which was found as the response to this question:
Insert a line at specific line number with sed or awk
My best guess is that sed is interpreting the command literally and evaluating it instead of copying it in. However, all of my attempts at forcing it to be evaluated simply as a string have failed. My attempts so far:
sed -i '2i\cd $var' FILE
sed -i '2i\cd \$var' FILE
sed -i "2i'cd $var'" FILE
and
Line='cd $var'
sed -i "2i$Line" FILE
I was fairly sure this last attempt would succeed, due to the hard quotes, but it still failed.
In fact, this also failed,
sed -i '2icd' FILE
Yet this succeeded (Just to confirm the general format):
sed -i '2ic' FILE
Just to be clear, all 5 of the failed attempts yielded the same error: A blank line was inserted at the desired location.
sed -i "2 i\\
$var" file
need a escape NewLine normaly after the i and depending the OS/sed a space before and/or after the i also. Finaly, with double quote, escape the \
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).
I wan't to replace a string like Europe12 with Europe12_yesturday in a file. Without changing the Europe12-36 strings that also exists in the file.
I tried:
$basename=Europe12
sed -i 's/\b$basename\b/${basename}_yesterday/g' file.txt
but this also changed the Europe12-36 strings.
Require a space or end of line character:
sed 's/Europe12\([ ]|$\)/Europe12_yesturday\1/g' input
Manually construct the delimiter list you want instead of using \b, \W or \<. - is not part of the word characters (alphanumericals), so that's why this also matches your other string. So try something like this, expanding the list as needed: [-a-zA-Z0-9].
You can do it in 2 times:
sed -e 's/Europe12/Europe12_yesturday/g' -e 's/Europe12_yesturday-36/Europe12-36/g' file.txt
sed 's/\(Europe12[[:blank:]]\)/\1_yesturday/g;s/Europe12$/&_yesturday/' YourFile
[[:blank:]] could be completeted with any boundary you accept also like .,;:/]) etc (be carrefull of regex meaning of this char in this case)
It is little late to reply..
It can be achieved easily by "word boundary" notation (\<..\>)
sed -i 's/\<$basename\>/${basename}_yesterday/g' file.txt