sed command in make file with hard coded $ - linux

I have a sed command in my makefile like following ,purpose of the command is, it should replace string "listen $address" to "",but as a result it is replacing "listen $address" to "$address"
sed -i -e "s/listen $$address//" file.txt
Please suggest any solution.
I have checked many posts but there $ is used as regex variable ,in my case it needs to be treated as hardcoded string

Enclose your sed command in single quotes to avoid variable expansion within double quotes:
sed -e -i 's/listen $address//' file
You can use double quotes though by escaping the $:
sed -e -i "s/listen \$address//" file

Related

Sed error "sed: unmatched '/'"

I have a file containing data with the following format:
{"parameter":"toto.tata.titi", "value":"0/2", "notif":"1"}
I make a change on the file with sed:
sed -i "/\<$param\>/s/.*/$line/" myfile
which line variable is
{"parameter":"toto.tata.titi", "value":"0/2", "notif":"3"}
and param variable is toto.tata.titi
The above sed command return error:
sed: unmatched '/'
Because the line variable is containing / ==> "0/2"
How to update my sed command to make it work even if the line variable is containing /?
Your $param or $line may contain / on it which causes sed to have a syntax error. Consider using other delimiters like | or #.
Example:
sed -i "/\<$param\>/s|.*|$line|" myfile
But that may not be enough. You can also quote your slashes when they expand:
sed -i "/\<${param//\//\\/}\>/s|.*|$line|" myfile
Other safer characters can be used too:
d=$'\xFF' ## Or d=$(printf '\xFF') which is compatible in many shells.
sed -i "/\<${param//\//\\/}\>/s${d}.*${d}${line}${d}" myfile
Check if your $param or $line variables have \n (newline), in my case, that was the reason of the error.
i had the same error when i used sed -i.bak 's/\r$//' command in my ansible task. i solved it by simply escaping the '\' in '\r'. i changed the command to sed -i.bak 's/\\r$//'
This will robustly only operate on lines containing the string (not regexp) toto.tata.titi:
awk -v param="$param" -v line="$line" 'index($0,param){$0 = line} 1' file
and will be unaffected by any chars in param or in line other than backslashes. If you need to be able to process backslashes as-is, the shell variables just need to be moved to the file name list and the awk variables populated from them in the BEGIN section instead of by -v assignment:
awk 'BEGIN{param=ARGV[1]; line=ARGV[2]; ARGV[1]=ARGV[2]=""} index($0,param){$0 = line} 1' "$param" "$line" file

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

search and replace string using sed

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.

Arguments inside sed loop

Im trying to understand sed command and loops.
I need to take part of the text (20 lines) and append it to the csv with the file name.
here is my code
for i in ~/mspl/*.doc
do
catdoc "$i" > auto.txt
sed -e '1,20!d' auto.txt > auto1.txt
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
sed -e '20s/$/~/' auto2.txt > auto3.txt
cat auto3.txt >> lines.csv
done
the problem is that my second "i" argument doesn't convert to the file name in csv.
in the line
sed -e '1s/^/"$i" ;/' auto1.txt > auto2.txt
Please tell what is my mistake here?
The issue is that variables are not expanded within single quotes '. Use double quotes instead ":
# this is assuming you did want to add explicit " around your $i
sed -e "1s/^/\"$i\" ;/" auto1.txt > auto2.txt
If your double-quoted string needs to contain explicit double quotes, you have to escape them (\"). If using double quotes is not an option (e.g. if you have a very complex command with multiple levels of quoting already) you can always exit the quoting around your variable:
sed -e '1s/^/"'$i'" ;/' auto1.txt > auto2.txt
You might find it useful to run this script with the -x setting, to see what's actually going on. (Just do set -x.)
But just to be blunt, what's going on is that your $i is inside single quotes, so it doesn't get expanded.

Resources