Grep: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Linux")
 
No edit summary
Line 2: Line 2:


* [[Linux#Commands|Linux]]
* [[Linux#Commands|Linux]]
* [[sed]]
=Overview=
<tt>grep</tt> 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).
<pre>
    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
</pre>
!!!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)"
!!!Regular Expressions
{{{
^ - beginning of line
$ - end of line
\t - tab
[0-9]*
\. dot
}}}
!!!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='
}}}
!!!-n
Prefixes the line with the ''line number''.
See [sed#InsertALineAppendInASpecificPositionLineNumberInAFile]
!!!Characters that Need Escaping
Same as [sed#SpecialCharactersNeedToBeEscapedInRegularExpressions]
!!!Return Only the First Match
{{{
-m 1
}}}
__Referenced by:__\\
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Revision as of 03:00, 16 February 2016

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


!!!Including TWO expressions at the same time

grep 'processor\

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

grep ' logger: [1-9]\+\.\

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


!!!Regular Expressions

{{{

^ - beginning of line

$ - end of line

\t - tab

[0-9]*

\. dot

}}}

!!!Excluding TWO expressions at the same time

grep -v "blah" 

!!!Do not Send Anything to stout

... and avoid > /dev/null:

{{{

   grep -q

}}}


!!!--color

{{{ grep --color 'context.layer=' }}}

!!!-n

Prefixes the line with the line number.


See [sed#InsertALineAppendInASpecificPositionLineNumberInAFile]

!!!Characters that Need Escaping

Same as [sed#SpecialCharactersNeedToBeEscapedInRegularExpressions]


!!!Return Only the First Match

{{{ -m 1 }}}



__Referenced by:__\\ [{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]