Bash and grep: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
Line 1: Line 1:
<metadesc>Basic grep examples.</metadesc>
<metadesc>Basic grep examples.</metadesc>
=== Common grep switches ===
=== Common grep switches ===
Show line number of match
==Show line number of match==
<pre>
<pre>
grep -n <string> <file(s)>
grep -n <string> <file(s)>
</pre>
</pre>


Insensitive match
==Insensitive match==
<pre>
<pre>
grep -i <string> <file(s)>
grep -i <string> <file(s)>
</pre>
</pre>


Before and after # lines from match
==Before and after # lines from match==
<pre>
<pre>
grep -A# -B# <string> <file(s)>
grep -A# -B# <string> <file(s)>
</pre>
</pre>


==Regex example==
<pre>
grep -iE 'app[-]?(blue\|green)?'


Will match:
app
app-blue
app-green
</pre>
[[Category:Bash]]
[[Category:Bash]]

Latest revision as of 10:10, 8 July 2024

Common grep switches

Show line number of match

grep -n <string> <file(s)>

Insensitive match

grep -i <string> <file(s)>

Before and after # lines from match

grep -A# -B# <string> <file(s)>

Regex example

grep -iE 'app[-]?(blue\|green)?'

Will match:
app
app-blue
app-green