How to replace all the occurences of chars like '[' , '(' etc with '\[' , '\(' using "sed" in a file?
I have a file "say file.txt" with following contents:
Capsule arr[0] in state A
rate_ul/dl=(2000000/7000000)
Capsule RBx[0] in state
...
...
using sed operator how can i replace all occurences of [
with \[
, (
with \(
, ]
with \]
and so on.
Capsule arr\[0\] in state A
rate_ul/dl=\(2000000/7000000\)
Capsule RBx\[0\] in state
...
...
Using the substitue operator in "gvim" I am able to achieve the same result.
ie. if i use :1,$ s/\[/\\[/g
in the vi editor in command mode I see all the [
chars replaced with \[
.
However if I try to use the same substitue command in a shell script using a sed command, i am not able to achieve the same result. ie If i use the following command in a shell script I am not able to achieve the desired result:
sed "s/\[/\\[/g" $temp_file2 > $temp_file1
where $temp_file2 conatins the lines with '[' characte开发者_如何学编程rs and $temp_file1 should contain the replaced '\[' chars
sed 's/[][()]/\\&/g' infile > outfile
Output
$ sed 's/[][()]/\\&/g' infile
Capsule arr\[0\] in state A
rate_ul/dl=\(2000000/7000000\)
Capsule RBx\[0\] in state
gawk version
gawk '{gsub(/[()\]\[]/,"\\\\&")}1' file
精彩评论