I am trying to use TCLs builtin exec procedure to run the following sed shell command:
sed -i 's/"VALUE.${name}">.*</"VALUE.${name}">${value}</' ${dir}/imp.xml
However when I pass it to exec tcl errors out with
sed: -e expression #1, char 1: unknown command: `''
no idea how to interpret this.
I tried escaping the exec string:
exec {sed -i 's/"VALUE.${name}">.*</"VALUE.${name}">${value}</' ${dir}/imp.xml}
However this prevents the tcl variables from being expanded inside of the string.
Does anyone know what I need to do to get tcl to exec this sed program?
(my shell is csh if that is relevant)
The final solution involved 2 changes to the command string,
Escape all the double quote characters, (thanks #Chris Heithoff)
Single quotes are handled funny by TCL, replacing them with double quotes
(that are not escaped!) resolves the issue.
The final, working command string:
exec sed -i "s/\"VALUE.${name}\">.*</\"VALUE.${name}\">${alue}</" ${dir}/impl.xml
Each argument to the exec command must correspond to an individual argument at the shell command line, so enclosing everything in {} doesn't work.
Try this:, where all double quotes and dollar signs are escaped.
exec sed -i 's/\"VALUE.\${name}\">.*</\"VALUE.\${name}\">\${value}' \${dir}/impl.xml
Related
Im trying to egrep lines that contain nothing but a single occurrence of "Hihihihihihihi!", with arbitrarily many 'hi's
Here is what I write
egrep "^Hi(hi)*!$" myfile.txt
But it didn't work. After pressing enter, the command was displayed again:
egrep "^Hi(hi)*myfile.txt" mayflies.txt
Anyone can help me?
Thanks!
The shell is interpreting !$ to substitute the last argument of the previous commend.
To disable these shell substitutions, change the double quotes to single quotes.
egrep '^Hi(hi)*!$' myfile.txt
Alternatively, you can use the -x switch to match only whole lines, obviating the need for the ^ and $ characters, and thus avoiding the fatal !$ argument substitution:
egrep -x "Hi(hi)*!" myfile.txt
You don't say what shell, but I suspect the problem you have is that the exclamation mark (!) is extra special to the shell. You need to escape that:
egrep "^Hi(hi)*\!$" myfile.txt
Should work in most shells where that's true.
Changing the double quotes to single quotes is not enough for all shells, the exclamation is still special inside single quotes. I just tested all this in the tcsh, other shells will have differences.
try it with single quotes. I think the $ is being interpreted by BASH as something, not sure what:
egrep '^Hi(hi)*!$' myfile.txt
I am trying to use sed to replace the expression 'root'#'localhost' with 'root'#'%' with no success. Could someone please help me with the command?
I tried the following:
sed -i ’s#\’root\’#\’localhost\’#\’root\’#\’%\’#g’ xyz.sql
sed: -e expression #1, char 1: unknown command: `?'
sed -i -e ’s/localhost/%/g’ xyz_2616.sql
sed: -e expression #1, char 1: unknown command: `?'
First, make sure you're using a single quote. ’ (a.k.a. ’ or unicode 8217) is not the same as ASCII character 39, '.
Next, you can't escape single quotes inside single quotes. Here's an answer I wrote about that some time ago.
You can, however, put single quotes inside double quotes, or escape them outside your single quoted string. For example, either of the following might work:
sed -e "s/'root'#'localhost'/'root'#'%'/g" file.sql
or
sed -e 's/'\''root'\''#'\''localhost'\''/'\''root'\''#'%'\''/g' file.sql
Alternately, you could substitute just the portion you're interested in, trusting that it doesn't appear elsewhere on the same line:
sed -e '/root.#.localhost/s/localhost/%/' file.sql
The ’ character doesn't look like a single quote (') to me. Make sure that you are using single quotes.
The character you're actually typing is a "right single quote mark". What we refer to as a "single quote" is actually an "apostrophe".
The following should work:
sed -e 's/localhost/%/g' rice_2616.sql
or, your first alternatives but with double quotes to avoid having to escape the embedded single quotes (which I presume are apostrophes):
sed -e "s/'root'#'localhost'/'root’#'%'/g" rice_2616.sql
This is my simple shell script
sample.sh
LOCALCONFIGDIR="Install"
CONFLOC="$LOCALCONFIGDIR/server.conf"
echo "Enter Web Url:"
read weburl
echo "sed 's/^\(ServerName\)$/\1 "$weburl"/' "$CONFLOC
sed "'s/^\(ServerName\)$/\1 "$weburl"/' "$CONFLOC
When I run this code, I get the result in echo command as following.
sed 's/^\(ServerName\)$/\1 www.weburl.com/' Install/server.conf
But when executing sed command in the next line, It says the below error.
sed: -e expression #1, char 1: unknown command: `''
I tried the command produced in echo statement from Terminal screen, It is working. But Line number 5, doesn't working from shell script
You need to use one set of quotes, not two, and since you want the $weburl variable expanded, you need to use double quotes:
sed "s/^\(ServerName\)$/\1 $weburl/" "$CONFLOC"
That'll be OK as long as $weburl doesn't contain any slashes. If it does, you need to use a different character than /, such as %, to separate the parts of the substitute command:
sed "s%^\(ServerName\)$%\1 $weburl%" "$CONFLOC"
I need to execute a sed command with pattern /^03.06.2014/ in a .sh script & command line. The date is a variable not a constant. How could i implement this? When I use a variable inside a regex pattern, the command breaks. Do I need to escape something here? Any help is appreciated. Thanks!
date=$(date +%m.%d.%Y)
sed -n '/^$date/,$p' filename
Use double quotes for variable expansion in sed
sed -n "/^$date/,\$p" filename
You need to use double quotes to allow for shell expansion. You'll need to escape the $ meaning EOF.
sed -n "/^$date/,\$p" filename
i have a sed command like this for search and replace string inside a file:
sed -i -e 's/`db1`./`db2`./g' result/files1.sql
that is working fine to replace the db1 to db2 inside the file of: result/files1.sql
however when i change it to bash and variable format, it does not work.
sed -i -e "s/`${mydbname}`./`${mydbname2}`./g" "${mypath}"
i get error like:
./mycoolscript: line 241: db1: command not found
./mycoolscript: line 241: db2: command not found
any solution would be great.
If is something you need to replace, you will need to escape by . Here it is
sed -i -e "s/\`${mydbname}\`./\`${mydbname2}\`./g" "${mypath}"
Escape the backtick character
sed -i -e "s/\`${mydbname}\`./\`${mydbname2}\`./g" "${mypath}"
Bash treats the part within backticks as a command and first executes that.
Try this
sed -i -e "s/${mydbname}/${mydbname2}/g" "${mypath}"
There is one more way, of using single quotes for literals & double quotes only around variables/escape sequences.
sed -i -e 's/`'"${mydbname}"'`./`'"${mydbname2}"'`./g' "${mypath}"
Because of single quotes, you will not have to escape the special characters.
The trade-off between escaping special characters vs. using mix of single & double quotes would depend on number of special characters vs. number of variables.
If there are too many characters that would need escaping & less number of variables, I would prefer mix of single & double quotes.