bash:通过grep将单引号和双引号匹配在一起



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

'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

我可能误解了这个问题,但看起来你是在问如何正确地转义引号。只做:

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

最新更新