Difference between revisions of "Grep"

From wikieduonline
Jump to navigation Jump to search
 
(19 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
<code>~/.[[bash_profile]]</code>
 
<code>~/.[[bash_profile]]</code>
:<code>alias grep='grep --color=auto'</code>
+
:<code>alias grep='[[grep --color=auto]]'</code>
  
 
== Common options ==
 
== Common options ==
 
:<code>[[-o]], --only-matching</code>
 
:<code>[[-o]], --only-matching</code>
 
:<code>-a, --text                equivalent to --binary-files=text</code>
 
:<code>-a, --text                equivalent to --binary-files=text</code>
 +
:<code>[[-i]]</code>
 +
:<code>[[grep -I|-I]]</code> Ignore [[binaries]]
 +
:<code>[[-r]]</code>
 +
:<code>[[-v]]</code>
 +
:<code>[[-w]]</code>
 +
:<code>[[grep --exclude|--exclude]]</code>
 +
:<code>[[--exclude-dir]]</code>
 +
::<code>grep -r --color=always -n -H --exclude-dir='.*'</code>
 +
:<code>[[--line-buffered]]</code>
 +
  -H, --with-filename
  
 
== Examples ==
 
== Examples ==
 +
 +
* <code>[[grep -i]]r "YOUR TEXT" *</code>
 +
 
Search for a pattern: TEXT="OTHER_TEXT"  
 
Search for a pattern: TEXT="OTHER_TEXT"  
 
* <code>[[grep -o]] 'TEXT="\w*"' index.html</code>
 
* <code>[[grep -o]] 'TEXT="\w*"' index.html</code>
Line 40: Line 53:
 
== Related ==
 
== Related ==
 
* <code>[[findstr]]</code> ([[Windows]])
 
* <code>[[findstr]]</code> ([[Windows]])
 +
* [[ggrep]] [[macOS]]
 +
* <code>[[stdin]], [[stdout]], [[stderr]], 2>[[&1]]</code>
 
* [[include (Cisco IOS)]]
 
* [[include (Cisco IOS)]]
* [[awk]]
+
* <code>[[awk]] '/TEXT_TO_SEARCH/{f=1}f' file</code>
 
* <code>[[ag]]</code>
 
* <code>[[ag]]</code>
 
* <code>[[git grep]] "your_string" $([[git rev-list --all]])</code>
 
* <code>[[git grep]] "your_string" $([[git rev-list --all]])</code>
Line 48: Line 63:
 
* <code>[[psql -c]]</code>
 
* <code>[[psql -c]]</code>
 
* <code>[[pkill -x]]</code>
 
* <code>[[pkill -x]]</code>
 +
* [[Amazon CloudWatch Logs Insights]]
 
* [[Logs]]: [[ElasticSearch]] and [[OpenSearch]]
 
* [[Logs]]: [[ElasticSearch]] and [[OpenSearch]]
 +
* [[yq]]
 +
* <code>[[logcli]]</code> for [[Loki]]
 
<pre>
 
<pre>
 
  [[:alpha:]]
 
  [[:alpha:]]

Latest revision as of 14:53, 25 August 2024

grep[1] is a command for searching plain text data sets for lines that match a regular expression.


~/.bash_profile

alias grep='grep --color=auto'

Common options[edit]

-o, --only-matching
-a, --text equivalent to --binary-files=text
-i
-I Ignore binaries
-r
-v
-w
--exclude
--exclude-dir
grep -r --color=always -n -H --exclude-dir='.*'
--line-buffered
 -H, --with-filename

Examples[edit]

Search for a pattern: TEXT="OTHER_TEXT"

Search for a pattern: TEXT="OTHER _TE XT" including spaces:

  • grep -o 'TEXT="[ a-zA-Z0-9_]*"' index.html

Search in hidden folders:

  • grep -ir your-search * .[^.]*

End of line:

  • grep your-search$

Add line:

  • grep -qxF 'your text "additional text"' yourfile.txt || echo 'your text "additional text"' >> yourfile.txt


Related[edit]

 [[:alpha:]]
 [[:alnum:]]
[[:digit:]]

See also[edit]

  • http://man7.org/linux/man-pages/man1/grep.1p.html
  • Advertising: