grep: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
extracted manpage transcrip to a subpage; reference links
more notes
Line 1: Line 1:
<hide>
[[page type::manual]]
[[thing type::command]]
[[platform::Linux]]
</hide>
==Pages==
==Pages==
* {{l/manpage}} - the current [[manpage]]
* {{l/manpage}} - the current [[manpage]]
* [[/manpage]] - a nicely-formatted but old version of the grep manpage
* [[/manpage]] - a nicely-formatted but old version of the grep manpage
==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
==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.
*: '''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.
* 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 below
*: '''A:''' apparently "*" by itself; see examples above
==Notes==
If ''file'' is specified using wildcards (e.g. *.log or *), apparently the command-line parser expands this into a listing of all matching files before passing it to grep. This unfortunately can lead to the error "bash: /bin/grep: Argument list too long". Putting a backslash before the wildcard will prevent the parser from expanding it before passing to grep, but grep doesn't seem to have any wildcard-handling abilities. So as far as I can tell, there is no way to grep a really large directory (the one I'm looking at has 5014 files).


==Examples==
The -r option doesn't seem to work if a file extension is specified (e.g. "*.log" in the example above), though it does work with "*". There may be some syntax which needs to surround "*.log* -- or maybe this simply is not doable using the inline-expansion method of handling wildcards?
; 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


==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.
If ''file'' is specified using wildcards (e.g. *.log or *.*), apparently the command-line parser expands this into a listing of all matching files before passing it to grep. This unfortunately can lead to the error "bash: /bin/grep: Argument list too long". Putting a backslash before the wildcard will prevent the parser from expanding it before passing to grep, but grep doesn't seem to have any wildcard-handling abilities. So as far as I can tell, there is no way to grep a really large directory (the one I'm looking at has 5014 files).
==Links==
==Links==
* [http://www.gnu.org/software/grep/manual/ official documentation]: more current than the manpage
* [http://www.gnu.org/software/grep/manual/ official documentation]: more current than the manpage on this server
* {{wikipedia}}
* {{wikipedia}}

Revision as of 15:41, 8 April 2017

<hide> page type::manual thing type::command platform::Linux </hide>

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

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

If file is specified using wildcards (e.g. *.log or *), apparently the command-line parser expands this into a listing of all matching files before passing it to grep. This unfortunately can lead to the error "bash: /bin/grep: Argument list too long". Putting a backslash before the wildcard will prevent the parser from expanding it before passing to grep, but grep doesn't seem to have any wildcard-handling abilities. So as far as I can tell, there is no way to grep a really large directory (the one I'm looking at has 5014 files).

The -r option doesn't seem to work if a file extension is specified (e.g. "*.log" in the example above), though it does work with "*". There may be some syntax which needs to surround "*.log* -- or maybe this simply is not doable using the inline-expansion method of handling wildcards?

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.