Link Search Menu Expand Document

Table of contents

  1. 背景
  2. grep with color
  3. grep 的邏輯應用
    1. Grep OR Operator
    2. Grep AND
    3. Grep AND using Multiple grep command
    4. Grep NOT

背景

grep with color

  • activate: grep --color, means grep --color=always or grep --color=auto
  • de-activate: grep --color=never
  • $GREP_COLOR: match words color,see stackexchange, eg export GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
The GNU grep default colors are defined into grep.c:

/* The color strings used for matched text.
   The user can overwrite them using the deprecated
   environment variable GREP_COLOR or the new GREP_COLORS.  */
static const char *selected_match_color = "01;31";      /* bold red */
static const char *context_match_color  = "01;31";      /* bold red */

/* Other colors.  Defaults look damn good.  */
static const char *filename_color = "35";       /* magenta */
static const char *line_num_color = "32";       /* green */
static const char *byte_num_color = "32";       /* green */
static const char *sep_color      = "36";       /* cyan */
static const char *selected_line_color = "";    /* default color pair */
static const char *context_line_color  = "";    /* default color pair */
  • examples
    • grep -b --colour -n foo myfile

grep 的邏輯應用1

Grep OR Operator

  1. grep 'pattern1\|pattern2' filename
  2. grep -E 'pattern1|pattern2' filename
  3. egrep 'pattern1|pattern2' filename
  4. grep -e pattern1 -e pattern2 filename

Grep AND

  1. grep -E 'pattern1.*pattern2' filename
  2. grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

Grep AND using Multiple grep command

  • grep -E 'pattern1' filename | grep -E 'pattern2'

Grep NOT

grep -v 'pattern1' filename

  1. 7 Linux Grep OR, Grep AND, Grep NOT Operator Examples, by Ramesh Natarajan on October 21, 2011, thegeekstuff