与&QUOT更换引号;``"和"''" [英] Replacing quotation marks with "``" and "''"

查看:234
本文介绍了与&QUOT更换引号;``"和"''"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含许多标记的文件,但我想将其转换为TeX的使用。

I have a document containing many " marks, but I want to convert it for use in TeX.

TeX的使用2`标志着开始引号,和2'大关右引号。

TeX uses 2 ` marks for the beginning quote mark, and 2 ' mark for the closing quote mark.

我只希望更改这些时,在一行出现在偶数(比如有2个,4个或6 的就行了)。对于例如。

I only want to make changes to these when " appears on a single line in an even number (e.g. there are 2, 4, or 6 "'s on the line). For e.g.

"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)

我的句子从未跨越断行,所以没有必要去检查多行。

My sentences never break across lines, so there is no need to check on multiple lines.

有单引号几个报价,这样我就可以手动更改的。

There are few quotes with single quotes, so I can manually change those.

我怎样才能将这些?

推荐答案

下面是一个使用我的一行重复的 SED

Here's my one-liner using repeated sed's:

cat file.txt | sed -e 's/"\([^"]*\)"/`\1`/g' | sed '/"/s/`/\"/g' | sed -e 's/`\([^`]*\)`/``\1'\'''\''/g'

(注意:这将无法正常工作,如果目前已经有后台蜱(`)文件中,但另有应该做的伎俩)

(note: it won't work correctly if there are already back-ticks (`) in the file but otherwise should do the trick)

辗转回到蜱虫,现在适用于所有情况:

Removed back-tick bug by simplifying, now works for all cases:

cat file.txt | sed -e 's/"\([^"]*\)"/``\1'\'\''/g' | sed '/"/s/``/"/g' | sed '/"/s/'\'\''/"/g'

使用注释:

cat file.txt                           # read file
| sed -e 's/"\([^"]*\)"/``\1'\'\''/g'  # initial replace
| sed '/"/s/``/"/g'                    # revert `` to " on lines with extra "
| sed '/"/s/'\'\''/"/g'                # revert '' to " on lines with extra "

这篇关于与&QUOT更换引号;``"和"''"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆