Difference between revisions of "grep"
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
	
 (→Notes:  integrated most of these with the file-masks subpage)  | 
				|||
| (One intermediate revision by the same user not shown) | |||
| Line 11: | Line 11: | ||
; grep -r ''text_to_find'' *:find ''[text_to_find]'' by checking all files in the current folder or under it  | ; grep -r ''text_to_find'' *:find ''[text_to_find]'' by checking all files in the current folder or under it  | ||
| − | Note that you can't do e.g. <code>grep -r ''[text_to_find]'' *.log</code> because that will only search in folders named <code>''[something]''.log</code>  | + | Note that you can't do e.g. <code>grep -r ''[text_to_find]'' *.log</code> because that will only search in folders named <code>''[something]''.log</code>. [[/file masks|The reasons for this are complicated.]]  | 
| − | |||
==Questions==  | ==Questions==  | ||
* So, how do you grep for a string which contains spaces and quote characters? This is probably some standard feature of Linux Shell syntax which everyone knows, but I don't, and that's why this page needs examples.  | * So, how do you grep for a string which contains spaces and quote characters? This is probably some standard feature of Linux Shell syntax which everyone knows, but I don't, and that's why this page needs examples.  | ||
| Line 19: | Line 18: | ||
*: '''A:''' apparently "*" by itself; see examples above  | *: '''A:''' apparently "*" by itself; see examples above  | ||
==Notes==  | ==Notes==  | ||
| − | + | The backtick character ("`") seems to mean "insert the output of this command here" -- so if you're trying to search for a literal backtick, it needs to be escaped with "\" ("\`"). This comes up sometimes when searching for [[SQL]] phrases.  | |
| − | |||
| − | |||
| − | |||
==Links==  | ==Links==  | ||
* [http://www.gnu.org/software/grep/manual/ official documentation]: more current than the manpage on this server  | * [http://www.gnu.org/software/grep/manual/ official documentation]: more current than the manpage on this server  | ||
* {{wikipedia}}  | * {{wikipedia}}  | ||
Latest revision as of 15:13, 21 June 2022
Pages
Examples
- grep text_to_find *.log
 - find [text_to_find] by checking all the logfiles in the current folder
 - grep -r text_to_find *
 - find [text_to_find] by checking all files in the current folder or under it
 
Note that you can't do e.g. grep -r [text_to_find] *.log because that will only search in folders named [something].log. The reasons for this are complicated.
Questions
- So, how do you grep for a string which contains spaces and quote characters? This is probably some standard feature of Linux Shell syntax which everyone knows, but I don't, and that's why this page needs examples.
- A: Tentatively, you can surround a string with double-quotes and escape any quotes in the search-string with the backslash character. (Single quotes do not seem to work.)
 
 - Likewise, how do you search a group of files? Using "*.*" for FILE doesn't seem to work, and leaving the FILE argument blank tells grep to expect input from STDIN.
- A: apparently "*" by itself; see examples above
 
 
Notes
The backtick character ("`") seems to mean "insert the output of this command here" -- so if you're trying to search for a literal backtick, it needs to be escaped with "\" ("\`"). This comes up sometimes when searching for SQL phrases.
Links
- official documentation: more current than the manpage on this server
 - Wikipedia