Grep: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(24 intermediate revisions by the same user not shown)
Line 22: Line 22:
</pre>
</pre>


=Options=


==-q==


!!!Including TWO expressions at the same time
Quiet.


{{{
==-v, --invert-match==
cat /proc/cpuinfo | grep 'processor\|siblings'


processor : 0
Invert the sense of matching, to select non-matching lines.
siblings : 2
processor : 1
siblings : 2
}}}


The method works even if there are spaces and other regular expression characters within the alternative regexes:
==<tt>-e</tt>==


{{{
GNU grep uses <code>-e</code> to introduce a regular expression to match. Also see: {{Internal|Grep_Regular_Expressions|<tt>grep</tt> Regular Expressions}}
... | grep ' logger: [1-9]\+\.\|(concurrent mode failure)'
}}}


This will select either "logger: [[1-9]\+\." or "(concurrent mode failure)"
=Regular Expressions=


{{Internal|grep Regular Expressions|grep Regular Expressions}}


!!!Regular Expressions
=Recipes=


{{{
==Including TWO expressions at the same time==


^ - beginning of line
<pre>
cat /proc/cpuinfo | grep 'processor\|siblings'


$ - end of line
processor : 0
siblings : 2
processor : 1
siblings : 2
</pre>


\t - tab
The method works even if there are spaces and other regular expression characters within the alternative regexes:


[0-9]*
<pre>
... | grep ' logger: [1-9]\+\.\|(concurrent mode failure)'
</pre>


\. dot
This will select either "logger: [1-9]\+\." or "(concurrent mode failure)"


}}}
==Excluding TWO expressions at the same time==


!!!Excluding TWO expressions at the same time
<pre>
 
{{{
     echo "something" | grep -v "blah" | grep -v "blah2"
     echo "something" | grep -v "blah" | grep -v "blah2"
}}}
</pre>


!!!Do not Send Anything to stout
==Do not Send Anything to <tt>stout</tt>==


... and avoid > /dev/null:
... and avoid > /dev/null:


{{{
<pre>
 
     grep -q
     grep -q
</pre>


}}}
==--color==
 


!!!--color
<pre>
 
{{{
grep --color 'context.layer='
grep --color 'context.layer='
}}}
</pre>


!!!-n
==Prefix the Line with the "Line Number" (-n)==


Prefixes the line with the ''line number''.
<tt>-n</tt> prefixes the line with the ''line number''.


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[sed#Insert_a_Line.2FAppend_in_a_Specific_Position_.28line_number.29_in_a_file|sed: Insert a Line/Append in a Specific Position (line number) in a file]]
</blockquote>


See [sed#InsertALineAppendInASpecificPositionLineNumberInAFile]
==Return Only the First Match==


!!!Characters that Need Escaping
-m means "stop reading the file about num matches. So to return the first match:


Same as [sed#SpecialCharactersNeedToBeEscapedInRegularExpressions]
<pre>
-m 1
</pre>


==Print More Lines of Trailing Context after Each Match==


!!!Return Only the First Match
Use -A to print a number of lines of trailing context after each match:
 
{{{
-m 1
}}}


grep -A 10 "something" ./file.txt


==Iterate over Lines Selected by grep==


<syntaxhighlight lang='bash'>
IFS="$(printf '\n\r')";
for i in $(grep "something ${some_file}); do
  ...
done
IFS="$(printf ' \t\n')"
</syntaxhighlight>


__Referenced by:__\\
See also: {{Internal|Bash_Built-In_Variables#IFS|IFS}}
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Latest revision as of 23:41, 29 March 2024

Internal

Overview

grep accepts regular expressions. Both " " and ' ' can be used to enclose the expression to search for.

If the expression is found, the enclosing lines are sent to stdout the command returns 0 (success).

If the expression is NOT found, the command returns 1 (failure).


    if grep -q "some regex" ./file.txt; then
        # do something if expression is found
    else
        # do something else if the expression is not found
    fi

Options

-q

Quiet.

-v, --invert-match

Invert the sense of matching, to select non-matching lines.

-e

GNU grep uses -e to introduce a regular expression to match. Also see:

grep Regular Expressions

Regular Expressions

grep Regular Expressions

Recipes

Including TWO expressions at the same time

cat /proc/cpuinfo | grep 'processor\|siblings'

processor	: 0
siblings	: 2
processor	: 1
siblings	: 2

The method works even if there are spaces and other regular expression characters within the alternative regexes:

... | grep ' logger: [1-9]\+\.\|(concurrent mode failure)'

This will select either "logger: [1-9]\+\." or "(concurrent mode failure)"

Excluding TWO expressions at the same time

     echo "something" | grep -v "blah" | grep -v "blah2"

Do not Send Anything to stout

... and avoid > /dev/null:

    grep -q

--color

grep --color 'context.layer='

Prefix the Line with the "Line Number" (-n)

-n prefixes the line with the line number.

sed: Insert a Line/Append in a Specific Position (line number) in a file

Return Only the First Match

-m means "stop reading the file about num matches. So to return the first match:

-m 1

Print More Lines of Trailing Context after Each Match

Use -A to print a number of lines of trailing context after each match:

grep -A 10 "something" ./file.txt

Iterate over Lines Selected by grep

IFS="$(printf '\n\r')";
for i in $(grep "something ${some_file}); do
  ...
done
IFS="$(printf ' \t\n')"

See also:

IFS