bash - grep 匹配完全子字符串忽略正则表达式语法

标签 bash grep

<分区>

有什么方法可以让 grep 匹配精确字符串,而不是将其解析为正则表达式?或者是否有一些工具可以为 grep 正确转义字符串?

$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034

最佳答案

使用 grep -F 或 fgrep。

$ echo "1034" | grep -F $version # shouldn't match
$ echo "10.4" | grep -F $version
10.4

参见手册页:

   -F, --fixed-strings
         Interpret PATTERN as a list of fixed strings, separated
         by newlines, any of which is to be matched.

我正在寻找术语“文字匹配”或“固定字符串”。

(另见 Using grep with a complex stringHow can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?)

关于bash - grep 匹配完全子字符串忽略正则表达式语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621795/

相关文章:

linux - 删除列名时 SED 命令错误

linux - 删除linux中几乎所有的目录和文件

grep 不匹配包含问号的行

arrays - 从 Bash 函数返回数组

linux - 让 cron 作业每 30 分钟运行一次 - 使用 cron.hourly?

Bash:开始日期小于等于结束日期

regex - 使用 grep 查找以/dev/开头的每个单词,包含此后的任何字符,直到空格

bash - 编写一个 shell 脚本,在 1 行中查找并输出文件名和内容

php - 为什么将 docker-compose exec 的输出管道输出到 grep,会破坏它?

shell - 在 shell 脚本中优化 grep(或使用 AWK)