将引号替换为“``”和“”“”

发布于 2024-12-28 07:38:44 字数 588 浏览 1 评论 0原文

我有一个包含许多 " 标记的文档,但我想将其转换为在 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)

我的句子永远不会跨行,所以没有需要检查多行引号

这些引号。

,因此我可以手动更改

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

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

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.

How can I convert these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好菇凉咱不稀罕他 2025-01-04 07:38:44

这是我的一句台词,对我有用:

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

This is my one-liner which is works for me:

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

And there is long version of this one-liner with comments:

{
    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
    }
}

You may run this script by:

awk -f replace-quotes.awk input.txt >output.txt
一向肩并 2025-01-04 07:38:44

这是我使用重复 sed 的一行代码:(

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

注意:如果文件中已经存在反引号 (`),则它将无法正常工作,但否则应该可以解决问题)

编辑:

通过简化删除了反勾号错误,现在适用于所有情况:

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 "

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)

EDIT:

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'

With comments:

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 "
口干舌燥 2025-01-04 07:38:44

使用 awk

awk '{n=gsub("\"","&")}!(n%2){while(n--){n%2?Q=q:Q="`";sub("\"",Q Q)}}1' q=\' in

说明

awk '{
  n=gsub("\"","&") # set n to the number of quotes in the current line
}
!(n%2){ # if there are even number of quotes
  while(n--){ # as long as we have double-quotes
    n%2?Q=q:Q="`" # alternate Q between a backtick and single quote
    sub("\"",Q Q) # replace the next double quote with two of whatever Q is
  }
}1 # print out all other lines untouched' 
q=\' in # set the q variable to a single quote and pass the file 'in' as input

使用 sed

sed '/^\([^"]*"[^"]*"[^"]*\)*$/s/"\([^"]*\)"/``\1'\'\''/g' in

Using awk

awk '{n=gsub("\"","&")}!(n%2){while(n--){n%2?Q=q:Q="`";sub("\"",Q Q)}}1' q=\' in

Explanation

awk '{
  n=gsub("\"","&") # set n to the number of quotes in the current line
}
!(n%2){ # if there are even number of quotes
  while(n--){ # as long as we have double-quotes
    n%2?Q=q:Q="`" # alternate Q between a backtick and single quote
    sub("\"",Q Q) # replace the next double quote with two of whatever Q is
  }
}1 # print out all other lines untouched' 
q=\' in # set the q variable to a single quote and pass the file 'in' as input

Using sed

sed '/^\([^"]*"[^"]*"[^"]*\)*$/s/"\([^"]*\)"/``\1'\'\''/g' in
时间海 2025-01-04 07:38:44

这可能对您有用:

sed 'h;s/"\([^"]*\)"/``\1''\'\''/g;/"/g' file

说明:

  • 复制原始行 h
  • 替换成对的 "s/"\([^"]*\ )"/``\1''\'\''/g
  • 检查是否有奇数 ",如果找到则恢复到原始行 /"/g

This might work for you:

sed 'h;s/"\([^"]*\)"/``\1''\'\''/g;/"/g' file

Explanation:

  • Make a copy of the original line h
  • Replace pairs of "'s s/"\([^"]*\)"/``\1''\'\''/g
  • Check for odd " and if found revert to original line /"/g
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文