【问题标题】:bash: matching single and double quotes together by grepbash:通过grep匹配单引号和双引号
【发布时间】:2021-02-13 09:53:34
【问题描述】:

目标是匹配文本中的单引号或双引号单词。

'foo'
"bar"
'buz"
# text

在经典 Shell 脚本(脚本 1)一书中的 bash 脚本中,需要转义引号。该怎么做?

#! /bin/sh - 
grep \(["']\).*\1 text
# script 1

另一种解决方案(脚本 2)是。

#! /bin/sh -
grep -e "'.*'" -e '".*"' text
# script 2

@William Pursell 的确切解决方案(脚本 3)更紧凑。

#! /bin/sh - 
grep '\(["'"']\).*\1" text
# script 3

【问题讨论】:

    标签: shell double-quotes single-quotes


    【解决方案1】:

    我可能误解了这个问题,但您似乎在问如何正确地转义引号。做吧:

    grep '\(["'"']\).*\1.*\1"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      相关资源
      最近更新 更多