根据https://tex.stackexchange.com/questions/48933/which-symbols-need-to-be-escaped-in-context的信息,我想准备一份用于ConTeXt的文件。我需要做几次替换:
#
替换为\#
。%
替换为\percent
。|
替换为\textbar
。$
替换为\textdollar
。_
替换为\textunderscore
。~
替换为\textasciitilde
。{
替换为\textbraceleft
。}
替换为\textbraceright
。我尝试使用Replacing "#", "$", "%", "&", and "_" with "\#", "\$", "\%", "\&", and "\_"中的信息来执行这些替换:
sed -i 's/\&/\\\&/g' ./File.csv
sed -i 's/\#/\\\#/g' ./File.csv
sed -i 's/\%/\\\percent/g' ./File.csv
sed -i 's/\|/\\\textbar/g' ./File.csv
sed -i 's/\$/\\\textdollar/g' ./File.csv
sed -i 's/\_/\\\textunderscore/g' ./File.csv
sed -i 's/\~/\\\textasciitilde/g' ./File.csv
sed -i 's/\{/\\\textbraceleft/g' ./File.csv
sed -i 's/\}/\\\textbraceright/g' ./File.csv
不幸的是,当我运行这些脚本时,整个文件被更改为一堆奇怪的字母,数字和“extbar”字样。
答案 0 :(得分:1)
这可能对您有用:
cat <<\! >Village.sed
s/&/\\&/g
s/#/\\#/g
s/%/\\percent/g
s/|/\\textbar/g
s/\$/\\textdollar/g
s/_/\\textunderscore/g
s/~/\\textasciitilde/g
s/{/\\textbraceleft/g
s/}/\\textbraceright/g
!
sed -f Village.sed ./File.csv
不确定为什么“extbar”出现在您的文件中,可能与s/\|/\\\textbar/g
行\|
意味着交替的行有关。
见这里:
echo foo | sed 's/\|/\\bar/'
\barfoo
echo foo | sed 's/|/\\bar/'
foo
答案 1 :(得分:1)
使用四个反斜杠代替逃脱。它们被评估两次。接下来,您将替换字符\t
,后跟字符串'extbar'
(来自\ textbar)
答案 2 :(得分:1)
当你做
时sed -i 's/|/\\\textbar/g' ./File.csv
sed reads it as s/|/\\\textbar/g \\ becomes \ and \t becomes tab character.
尝试
sed -i "s/|/\\\textbar/g"
或
sed -i 's/|/\\textbar/g'