我有一个包含很多内容的文档"
标记,但我想将其转换为在 TeX 中使用。
TeX 使用 2 个 ` 标记作为开始引号,使用 2 个 ' 标记作为结束引号。
我只想在以下情况下对这些进行更改"
以偶数出现在单行上(例如,有 2、4 或 6"
已上线)。例如
"This line has 2 quotation marks."
--> ``This line has 2 quotation marks.''
"This line," said the spider, "Has 4 quotation marks."
--> ``This line,'' said the spider, ``Has 4 quotation marks.''
"This line," said the spider, must have a problem, because there are 3 quotation marks."
--> (unchanged)
我的句子永远不会跨行,因此不需要检查多行。
单引号很少,所以我可以手动更改它们。
我怎样才能转换这些?
这是我的一句台词,对我有用:
awk -F\" '{if((NF-1)%2==0){res=$0;for(i=1;i<NF;i++){to="``";if(i%2==0){to="'\'\''"}res=gensub("\"", to, 1, res)};print res}else{print}}' input.txt >output.txt
这段话有一个长版本,附有评论:
{
FS="\"" # set field separator to double quote
if ((NF-1) % 2 == 0) { # if count of double quotes in line are even number
res = $0 # save original line to res variable
for (i = 1; i < NF; i++) { # for each double quote
to = "``" # replace current occurency of double quote by ``
if (i % 2 == 0) { # if its closes quote replace by ''
to = "''"
}
# replace " by to in res and save result to res
res = gensub("\"", to, 1, res)
}
print res # print resulted line
} else {
print # print original line when nothing to change
}
}
您可以通过以下方式运行此脚本:
awk -f replace-quotes.awk input.txt >output.txt
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)