Monday, May 6, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 376  / 2 Years ago, sat, april 9, 2022, 5:12:23

I'm searching for lines with $ and consecutively ' or " through this grep in my terminal:



grep "$('|")" -rin folder_path


After pressing enter the terminal doesn't think the line is complete so it adds another line for me to keep writing. If I press Tab while writing folder_path it shows this error:



bash: command substitution: line 107: syntax error: file premature end
bash: command substitution: line 106: unexpected file premature end while searching for `''


Those error messages were translated because a part of them are shown in portuguese. I'm using Ubuntu 13.10.



What is wrong?


More From » command-line

 Answers
2

Use this:



grep -rn "$('|")" /path/to/directory


Your problem is actually two-fold :




  • From grep's perspective, You are using the Extended Regex syntaces i.e. (, |, ) without escaping them (to treat them special) inside your Basic Regex pattern. To overcome this either escape all those to treat them special or use the -E option of grep.


  • Another problem caused by the above not escaping issue is that $() is the bash command substitution pattern, as you have not escaped the relevant characters bash was treating the pattern $() as command substitution. Note that the $() actually makes the command plus command substitution as first will make the second literal and the remaining command substiturion pattern will be left as it is.




Also note that -i is needless here as we are not searching alphabatic characters. You might be interested in printing the file names :



grep -Hrn "$('|")" /path/to/directory


Another thing is that you could get away without escaping $ (indicates the end of line) in this case becasue there are characters to match after $, as a result grep will treat it literally :



grep -Hrn "$('|")" /path/to/directory

[#19938] Sunday, April 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
naldis

Total Points: 257
Total Questions: 101
Total Answers: 111

Location: Kenya
Member since Sat, Feb 25, 2023
1 Year ago
;