This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I want to use sed on some text that contains backslashes, and want to avoid lots of escaped \/ characters.
How can I change the delimiter or separator character to be other than /?
In GNU sed, simply swap out the / for the character you wish to use:
% echo /// | sed 's_/_x_g'
xxx
\ can also be used, and must not be escaped (it fails when escaped as \\):
% echo xxx | sed 's\x\y\g'
yyy
Related
This question already has answers here:
How do I replace single quotes with another character in sed?
(6 answers)
Closed 5 months ago.
Trying to put ' before each line of text and ' at the end of each line of text.
I have been using sed 's/^/1/' file.txt to replace to begging of each line and sed 's/$/0/' file.txt to replace the end of each line.
What I am trying to make work is sed 's/^/'/' and sed 's/$/'/'
This would format my file to make each line reach as a command, when applied to a separate script.
echo abc | sed "s/.*/'&'/"
Output:
'abc'
From man sed:
The replacement may contain the special character & to refer to that portion of the pattern space which
matched
This question already has answers here:
How to escape the ampersand character while using sed
(3 answers)
Closed 2 years ago.
My config file looks like:
KEY1=VALUE1
URL=https://drive.google.com/uc?export=download&id=myhash
KEY3=VALUE3
I'm trying to use sed to replace the URL value with another one. I got to the following:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download&id=mynewhash#g' file.txt
But that doesn't seem to work, as I'm getting:
URL=https://drive.google.com/uc?export=downloadURL=https://drive.google.com/uc?export=download&id=mynewhash=myhash
What am I missing? Thanks
& is a special character in the replacement string provided to the s command of sed. It represents the string that matches the entire regex used to search (URL=.* in your example).
In order to represent itself it needs to be escaped with \:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download\&id=mynewhash#g' file.txt
Type man sed in your terminal to read its documentation or read the documentation of sed online.
This question already has an answer here:
Escaping forward slashes in sed command [duplicate]
(1 answer)
Closed 4 years ago.
I want to replace special character "/" with a special character "*" using sed command.
EXAMPLE-
I / YOU.
I * YOU.
As detailed in the comment Escaping forward slashes in sed command
You can use instead of
sed "s/target/replacement/" file
either
sed "s|target|replacement|" file
or
sed "s#target#replacement#" file
Command:
$ echo "I / YOU." | sed 's#/#*#'
I * YOU.
More generally when looking at the sed accepted syntax:
sed
[2addr] s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the
pattern space. Any character other than backslash or newline can be
used instead of a slash to delimit the BRE and the replacement. Within
the BRE and the replacement, the BRE delimiter itself can be used as a
literal character if it is preceded by a backslash.
You can also go for another approach in which you do not change the separators but you use the hexadecimal value of the character you want to replace, this will also avoid ambiguity. (http://www.asciitable.com/)
$ echo "I / YOU." | sed 's/\x2F/*/'
I * YOU.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 7 years ago.
I have a stumbling block with sed replace (Linux shell)
I need to replace
</test>
to
</test1>
tried
sed -i 's/<\/test>/</test1>/g'
and similar variants -but still no luck...so thanks for any hint to try
Try this:
echo '</test>' | sed 's|</test>|</test1>|'
For what you tried, you need to escape the slash in the replacement string:
sed -i 's/<\/test>/<\/test1>/g'
Or change the regex boundary marker character:
sed -i 's%</test>%</test1>%g'
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
Changing the delimiter slash (/) to pipe (|) in the substitute command of sed works like below
echo hello | sed 's|hello|world|'
How can I change the delimiter slash (/) to pipe (|) in the sed insert command below?
echo hello | sed '/hello/i world'
I'm not sure what is intended by the command you mentioned:
echo hello | sed '/hello/i world'
However, I presume that you want to perform certain action on lines matching the pattern hello. Lets say you wanted to change the lines matching the pattern hello to world. In order to accomplish that, you can say:
$ echo -e "something\nhello" | sed '\|hello|{s|.*|world|}'
something
world
In order to match lines using a regexp, the following forms can be used:
/regexp/
\%regexp%
where % may be replaced by any other single character (note the preceding \ in the second case).
The manual provides more details on this.
The answer to the question asked is:
echo hello | sed '\|hello|i world'
That is how you would prepend a line before a line matching a path, and avoid Leaning Toothpick Syndrome with the escapes.