Using sed from makefile to replace a string with a $ in it [duplicate] - sed

This question already has answers here:
Write Dollar sign in sed command pattern inside makefile
(2 answers)
Closed 5 years ago.
I have a simple sed replacement for a text file:
sed -i 's/Temp_3/$t0/g' ./somefile.txt
When I use it in a bash script everything works fine.
However, if I try to use the same line from a makefile,
the $t is (probably) expanded into the empty string
(there is no variable called t in the makefile) and
Temp_3 is replaced with 0. What is the best way to solve this?
Thanks!

What happens is that make first does its substitutions,
in your case $t with nothing, then runs the command.
You can, or should ;), circumvent this problem by changing your command as follows: change $ with $$;
make will substitute the double $ sign with a single one.
sed -i 's/Temp_3/$$t0/g' ./somefile.txt

Related

sed -i on FreeBSD [duplicate]

This question already has answers here:
sed in-place flag that works both on Mac (BSD) and Linux
(15 answers)
Closed 4 years ago.
I have got strings stored in two variables (line is current line and new is the replacement). My code looks like this :
sed -i "s#line#new#" output_file
However, this solution does not work on FreeBSD.
Is there any way to modify this code just a bit so it would work?
Yes. Use:
sed -i '' "s#line#new#" output_file
On BSD systems (and macOS too), the sed command's -i option requires a suffix, which may be attached to the -i or a separate argument. However, when the suffix is empty, it must be a separate argument. This is different from GNU sed, where the -i option takes an optional suffix, but if specified, it must be attached to the -i option. Scripts portable between the two (BSD and GNU) therefore must be written with an explicit non-empty suffix attached to the -i option. Note that such scripts may still be unportable to other POSIX systems; the -i option is not standardized (as you can tell from the divergent behaviour).

Replace string with a slash character with sed [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 5 years ago.
I have a directory's tree with several files inside.
I want to replace in every file, the string index.html for a character /.
Since / is a special character, How can I instruct to the linux "sed" command so I can change all files that I have under a directory?
Thanks!
Sed is usualy used with / as a separator. Actually you can use any symbol as a separator, sed just uses the first symbol after s command for that. For example sed 's|/|slash|'. Or you can escape / like that sed s/\//slash/. It's dangerous command, be careful, make a backup of your dir:
for file in `find /your/dir/ -type f`; do sed -i "$file" 's|index.html|/|g'; done

sed: -e expression #1, char 23: unknown option to `s' [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 7 years ago.
I'm trying to use sed to update a config file using a bash script. I have a similar sed command right above this one in the script that runs fine. I can't seem to figure out why this is breaking:
sed -i.bak \
-e s/"socketPath:'https://localhost:9091'"/"socketPath:'/socket'"/g \
$WEB_CONF
Any ideas?
The quotes and double quotes are causing problems.
You are using them in view of the slashes in the string.
In sed you can use another delimiter, such as a #.
sed -e 's#socketPath:https://localhost:9091#socketPath:/socket#g' \
$WEB_CONF
Escape your slashes in the pattern or use a different delimiter like this:
sed -i.bak \
-e s%"socketPath:'https://localhost:9091'"%"socketPath:'/socket'"%g \
$WEB_CONF
I am confused looking at the delimiters you have used so you can't blame sed for goofing up. When your dataset has / in them, it is often advised to use a different delimiters like # or _ (yes, sed supports various delimiters).
Also, your quoting looks abit off. The syntax should be:
sed -i.bak 's#substitute#replacement#g' "$WEB_CONF"
If your substitute and/or replacement has variables use " instead ' which will allow it to interpolate.

How to insert strings containing slashes with sed? [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I have a Visual Studio project, which is developed locally. Code files have to be deployed to a remote server. The only problem is the URLs they contain, which are hard-coded.
The project contains URLs such as ?page=one. For the link to be valid on the server, it must be /page/one .
I've decided to replace all URLs in my code files with sed before deployment, but I'm stuck on slashes.
I know this is not a pretty solution, but it's simple and would save me a lot of time. The total number of strings I have to replace is fewer than 10. A total number of files which have to be checked is ~30.
An example describing my situation is below:
The command I'm using:
sed -f replace.txt < a.txt > b.txt
replace.txt which contains all the strings:
s/?page=one&/pageone/g
s/?page=two&/pagetwo/g
s/?page=three&/pagethree/g
a.txt:
?page=one&
?page=two&
?page=three&
Content of b.txt after I run my sed command:
pageone
pagetwo
pagethree
What I want b.txt to contain:
/page/one
/page/two
/page/three
The easiest way would be to use a different delimiter in your search/replace lines, e.g.:
s:?page=one&:pageone:g
You can use any character as a delimiter that's not part of either string. Or, you could escape it with a backslash:
s/\//foo/
Which would replace / with foo. You'd want to use the escaped backslash in cases where you don't know what characters might occur in the replacement strings (if they are shell variables, for example).
The s command can use any character as a delimiter; whatever character comes after the s is used. I was brought up to use a #. Like so:
s#?page=one&#/page/one#g
A very useful but lesser-known fact about sed is that the familiar s/foo/bar/ command can use any punctuation, not only slashes. A common alternative is s#foo#bar#, from which it becomes obvious how to solve your problem.
add \ before special characters:
s/\?page=one&/page\/one\//g
etc.
In a system I am developing, the string to be replaced by sed is input text from a user which is stored in a variable and passed to sed.
As noted earlier on this post, if the string contained within the sed command block contains the actual delimiter used by sed - then sed terminates on syntax error. Consider the following example:
This works:
$ VALUE=12345
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
MyVar=12345
This breaks:
$ VALUE=12345/6
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
sed: -e expression #1, char 21: unknown option to `s'
Replacing the default delimiter is not a robust solution in my case as I did not want to limit the user from entering specific characters used by sed as the delimiter (e.g. "/").
However, escaping any occurrences of the delimiter in the input string would solve the problem.
Consider the below solution of systematically escaping the delimiter character in the input string before having it parsed by sed.
Such escaping can be implemented as a replacement using sed itself, this replacement is safe even if the input string contains the delimiter - this is since the input string is not part of the sed command block:
$ VALUE=$(echo ${VALUE} | sed -e "s#/#\\\/#g")
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
MyVar=12345/6
I have converted this to a function to be used by various scripts:
escapeForwardSlashes() {
# Validate parameters
if [ -z "$1" ]
then
echo -e "Error - no parameter specified!"
return 1
fi
# Perform replacement
echo ${1} | sed -e "s#/#\\\/#g"
return 0
}
this line should work for your 3 examples:
sed -r 's#\?(page)=([^&]*)&#/\1/\2#g' a.txt
I used -r to save some escaping .
the line should be generic for your one, two three case. you don't have to do the sub 3 times
test with your example (a.txt):
kent$ echo "?page=one&
?page=two&
?page=three&"|sed -r 's#\?(page)=([^&]*)&#/\1/\2#g'
/page/one
/page/two
/page/three
replace.txt should be
s/?page=/\/page\//g
s/&//g
please see this article
http://netjunky.net/sed-replace-path-with-slash-separators/
Just using | instead of /
Great answer from Anonymous. \ solved my problem when I tried to escape quotes in HTML strings.
So if you use sed to return some HTML templates (on a server), use double backslash instead of single:
var htmlTemplate = "<div style=\\"color:green;\\"></div>";
A simplier alternative is using AWK as on this answer:
awk '$0="prefix"$0' file > new_file
You may use an alternative regex delimiter as a search pattern by backs lashing it:
sed '\,{some_path},d'
For the s command:
sed 's,{some_path},{other_path},'

How to escape the forward slash in a sed command [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I'm writing a shell script with this command:
sed -e 's/OLD_ITEM/NEW_ITEM/g'
But I actually want to do something that includes a directory:
sed -e 's/FOLDER/OLD_ITEM/NEW_ITEM/g'
How do ignore the forward slash so that the entire line FOLDER/OLD_ITEM is read properly?
You don't have to use / as delimiter in sed regexps. You can use whatever character you like, as long as it doesn't appear in the regexp itself:
sed -e 's#FOLDER/OLD_ITEM#NEW_ITEM#g'
or
sed -e 's|FOLDER/OLD_ITEM|NEW_ITEM|g'
You need to escape the / as \/.
The escape (\) preceding a character tells the shell to interpret that character literally.
So use FOLDER\/OLD_ITEM
Escape it !
sed -e 's/FOLDER\/OLD_ITEM/NEW_ITEM/g'