Bash 如何在shell中搜索包含括号和其他特殊字符的模式

Bash 如何在shell中搜索包含括号和其他特殊字符的模式,bash,shell,unix,scripting,grep,Bash,Shell,Unix,Scripting,Grep,我正在尝试在“output2”文件中执行下面的模式搜索,但即使grep命令的状态显示成功,它也不会给出所需的输出 ➜ automate git:(master) ✗ grep -F "@PreAuthorize("isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')")" output2 ➜ automate git:(master) ✗ grep -nF "@PreAut

我正在尝试在“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
/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
我将“替换为\”,它解决了这个问题

grep -F "@PreAuthorize(\"isFullyAuthenticated() and hasPermission(#updateStatus, 'MAX_LOAN_AMOUNT_FOR_APPROVE')\")" output2

是否尝试使用单引号引用字符串?是否尝试使用单引号引用字符串?不,不需要对字符串使用反斜杠grep@Zack你是对的,grep不需要它,但这不是它存在的原因:需要反斜杠,以便shell将单引号视为一个文字字符,因此传递把它交给格雷普。正确使用反斜杠是正确的。但对于我的问题,这不是必需的。:)如果不使用带双引号的反斜杠,它将不起作用。@Zack您的解决方案在这种情况下确实起作用,但需要注意:shell将对带双引号的字符串进行变量、命令和算术展开。如果你想确保你的字符串是按字面意思理解的,单引号更安全。不需要对单引号使用反斜杠grep@Zack你是对的,grep不需要它,但这不是它存在的原因:需要反斜杠,以便shell将单引号视为文字字符,从而将其传递给grep。正确使用反斜杠是正确的。但对于我的问题,这不是必需的。:)如果不使用带双引号的反斜杠,它将不起作用。@Zack您的解决方案在这种情况下确实起作用,但需要注意:shell将对带双引号的字符串进行变量、命令和算术展开。如果你想确保你的字符串是字面意思,单引号更安全。