Zack
Zack

Reputation: 2208

How to search for pattern containing brackets and other special characters in shell

I am trying to do the below pattern search in the "output2" file but it does not give the desired output even though the status of the grep command shows success.

➜  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) ✗ 

Contents of the output2 file below:

/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,

Upvotes: 0

Views: 87

Answers (2)

Zack
Zack

Reputation: 2208

I replaced " with \" and it solved the problem .

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

Upvotes: 0

John1024
John1024

Reputation: 113924

The string needs to be properly quoted:

$ 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')")

The shell treats all characters inside of a single quoted string as literal characters except for a single quote. To enter a single quote as part of the string, use '\''.

'\'' works in three steps: first it terminates the single quoted string with ', then it adds an escaped single-quote with \', and lastly it starts a new single-quoted string with '.

Upvotes: 4

Related Questions