How to set sed command correctly - linux

When I try following command, I'd like to rewrite sql.
Day='2020/12/1'
Dir=/home/test/data
sql=`cat $Dir"/"$test".sql" | sed -e "s/Day/$Day/g"`
I suffered following errors.
sed: -e expression #1, char 24: unknown option to `s'
Why the s is recognised as option ? why is this command couldnt work well ?
if someone has opinoin, please let me know
Thanks

The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.
Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string like below:
sql=`cat $Dir"/"$test".sql" | sed -e "s|Day|$Day|g"`
Also, you would need to use sed -i to update the file in-place, since it looks like that is what you're trying to do.

Related

How to replace a string in multiple files in linux command

I need to replace a string in a lot of files in a folder, with only ssh access to the server. How can I do this?
for example i want replace all files Which contains code
<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>
I want replace it with my name: sultan
I do something like this:
sed -i 's/<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>/sultan/g' *
but the problem i see error message in linux commands:
sed: -e expression #1, char 20: unknown option to `s'
How do I fix this problems?
There are two errors:
the slash is used as a delimiter in your call to sed, so this ambiguity needs to be resolve
you're using single quotes in the search term but also to enclose the sed parameter.
You can try something like this instead:
sed -i "s|<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>|sultan|g" *

Unterminated `s' command with sed troubleshooting

I have a problem with sed. I want to replace the entire specific line number for multiple lines in multiples documents.
This the bash command for 1 specific line in 1 specific document:
BNAME=$(basename $FILE .pdb)
psfgen1="pdb ./sedpdb/${BNAME}.pdb/"
sed -i '8s/'.*'/'${psfgen1}'/' ./psfgen.inp
And I get this error :
sed: -e expression #1, char 60: unterminated `s' command
Is anyone know how to solve this issue? Thanks!
I can see two things wrong:
There are forward slashes in the string that you're attempting to use in the sed command. These will be interpreted as part of the command, so you should use a different delimiter.
The * is unquoted, so will be glob-expanded by the shell to the names of all the files in the directory.
Reliably using shell variables in string substitutions is non-trivial but can be done using one of the approaches shown in the answers to this question.
In your case, it looks like you can probably get away with using another character as the delimiter, such as #:
sed -i "8s#.*#${psfgen1}#" ./psfgen.inp

How to use sed with a variable that needs to be escaped

I have a file, and I am trying to use bask to replace all the contents of a substring with a path.
I can use the command:
sed -i s/{WORKSPACE}/$MYVARIABLE/g /var/lib/jenkins/jobs/MY-JOB/workspace/config/params.ini
My config/params.ini looks like:
[folders]
folder1 = {WORKSPACE}/subfolder1
folder2 = {WORKSPACE}/subfolder2
however, when $MYVARIABLE is a path, it fails (containing slashes), the sed command fails with:
sed: -e expression #1, char 16: unknown option to `s'
When I run through it manually, I see that the $MYVARIABLE needs to have it's path-slashes escaped. How can I modify my sed command to incorporate an escaped version of $MYVARIABLE?
There's nothing saying you have to use / as your delimiter. sed will use (almost) anything you stick in there. I have a tendency to use |, since that never (rarely?) appears in a path.
sridhar#century:~> export boong=FLEAK
sridhar#century:~> echo $PATH | sed "s|/bin|/$boong|g"
~/FLEAK:/usr/local/FLEAK:/usr/local/sbin:/usr/local/games:/FLEAK:/sbin:/usr/FLEAK:/usr/sbin:/usr/games:/usr/lib/lightdm/lightdm:/home/oracle/app/oracle/product/12.1.0/server_1/FLEAK
sridhar#century:~>
Using double-quotes will allow the shell to do the variable-substitution.
Just escape the $ sign, and use a different delimiter:
sed -i 's;{WORKSPACE};\$MYVARIABLE;g' your_file

How to use variable in sed command to replace a property value in a property file

i can able to use the below command to change value of target by means of hard coding.
sed -ie s/^target=.*/target=google.com/ url.properties
But if i used variable i am getting error. I dont know how sed commands all working. I only needed to set build system thats it.
url = google.com
sed -ie s/^target=.*/target=$url/ url.properties
the error is
sed: -e expression #1, char 25: unterminated `s' command
The problem may be happening because your URL may contain / which bash interprets as sed syntax, so something like https/www.google.com ends up something like :
sed -ie 's/^target=.*/target=https/www.google.com/' url.properties
I will suggest to delimit any special characters to avoid sed to be confused :
url=google.com
url=`echo $url | sed -e "s/\//\\\\\\\\\//g"` # delimits backslash in URL's
sed -ie "s/^target=.*/target=$url/" url.properties
Two problems:
You can't have spaces in variable assignments in bash
You must quote the sed command (and ideally the url too)
Working example:
url="google.com"
sed -ie "s/^target=.*/target=$url/" url.properties

Executing Sed Command

When I run the command
sed -e "s/$1/#root#The-Three-Little-Pigs-Siri-Proxy/" -iĀ gen_certs.sh
I Get the following error. I am trying to replace the text $1 with the other below in the same file, not creating a new one just modifying the current one.
sed: -e expression #1, char 0: no previous regular expression
Any ideas what could be causing the error and how to fix it?
OS: Ubuntu 10.10 32 bit
$1 will expand to a null-string('') if it's in double-qouted string.
You can use single quote to keep the literal value of$1:
sed -e 's/$1/#root#The-Three-Little-Pigs-Siri-Proxy/' -i gen_certs.sh
You need to escape the pattern: sed -e "s/\$1/#root#The-Three-Little-Pigs-Siri-Proxy/" -i gen_certs.sh, since $1 denotes a back reference in sed (presuming you want to replace the string $1 in your input, right?)

Resources