如何在 Bash/Grep 中转义单引号? [英] How to escape single quotes in Bash/Grep?
本文介绍了如何在 Bash/Grep 中转义单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 grep 搜索如下所示的字符串:
I want to search with grep for a string that looks like this:
something ~* 'bla'
我试过这个,但是shell删除了单引号argh..
I tried this, but the shell removes the single quotes argh..
grep -i '"something ~* '[:alnum:]'"' /var/log/syslog
什么是正确的搜索?
推荐答案
grep -i "something ~* '[[:alnum:]]*'" /var/log/syslog
对我有用.
- 转义第一个
*
以匹配文字*
而不是使其成为零个或多个匹配字符:~*
将匹配零次或多次出现的~
而~*
匹配something
之后的表达式 - 在
:alnum:
周围使用双括号(参见示例 这里) - 在
[[:alnum::]]
之后使用*
不仅可以匹配单引号之间的 一个 字符,还可以匹配其中的几个 - 单引号根本不需要转义,因为它们包含在受双引号限制的表达式中.
~*
- escape the first
*
to match a literal*
instead of making it the zero-or-more-matches character:
~*
would match zero or more occurrences of~
while
~*
matches the expression~*
aftersomething
- use double brackets around
:alnum:
(see example here) - use a
*
after[[:alnum::]]
to match not only one character between your single quotes but several of them - the single quotes don't have to be escaped at all because they are contained in an expression that is limited by double quotes.
这篇关于如何在 Bash/Grep 中转义单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文