如何在 shell 中搜索包含方括号和其他特殊字符的模式
How to search for pattern containing brackets and other special characters in shell
我正在尝试在 "output2" 文件中执行以下模式搜索,但它没有提供所需的输出,即使 grep 命令的状态显示成功。
➜ automate git:(master) ✗ grep -F "@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")" output2
➜ automate git:(master) ✗ grep -nF "@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")" output2
➜ automate git:(master) ✗ echo $?
0
➜ automate git:(master) ✗
以下 output2 文件的内容:
/home/workspace/OutputWithMessages.txt:1549:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:3254:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:4558:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5438:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5744:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:6986:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:7344:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus,
字符串需要正确引用:
$ grep -F '@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, '\''MAX_LOAN_AMOUNT_FOR_APPROVE'\'')")' output2
/home/workspace/OutputWithMessages.txt:1549:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:3254:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:4558:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5438:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5744:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:6986:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
shell 将单引号字符串内的所有字符视为文字字符 除了 用于单引号。要输入单引号作为字符串的一部分,请使用 '\''
.
'\''
分三步工作:首先用 '
终止单引号字符串,然后用 \'
添加转义单引号,最后开始一个新的带有 '
.
的单引号字符串
我把 " 换成 \" 就解决了问题。
grep -F "@PreAuthorize(\"isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')\")" output2
我正在尝试在 "output2" 文件中执行以下模式搜索,但它没有提供所需的输出,即使 grep 命令的状态显示成功。
➜ automate git:(master) ✗ grep -F "@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")" output2
➜ automate git:(master) ✗ grep -nF "@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")" output2
➜ automate git:(master) ✗ echo $?
0
➜ automate git:(master) ✗
以下 output2 文件的内容:
/home/workspace/OutputWithMessages.txt:1549:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:3254:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:4558:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5438:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5744:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:6986:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:7344:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus,
字符串需要正确引用:
$ grep -F '@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, '\''MAX_LOAN_AMOUNT_FOR_APPROVE'\'')")' output2
/home/workspace/OutputWithMessages.txt:1549:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:3254:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:4558:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5438:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:5744:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
/home/workspace/OutputWithMessages.txt:6986:+ @PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")
shell 将单引号字符串内的所有字符视为文字字符 除了 用于单引号。要输入单引号作为字符串的一部分,请使用 '\''
.
'\''
分三步工作:首先用 '
终止单引号字符串,然后用 \'
添加转义单引号,最后开始一个新的带有 '
.
我把 " 换成 \" 就解决了问题。
grep -F "@PreAuthorize(\"isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')\")" output2