Javatpoint Logo
Javatpoint Logo

Linux grep

The 'grep' command stands for "global regular expression print". grep command filters the content of a file which makes our search easy.

grep with pipe

The 'grep' command is generally used with pipe (|).

Syntax:

Example:

Linux Grep Filters1

Look at the above snapshot, grep command filters all the data containing '9'.


grep without pipe

It can be used without pipe also.

Syntax:

Example:

Linux Grep Filters2

Look at the above snapshot, grep command do the same work as earlier example but without pipe.


grep options

  • grep -vM: The 'grep -v' command displays lines not matching to the specified word.
  • Syntax:

    Example:

    Linux Grep Filters3

    Look at the above snapshot, command "grep -v 9 marks.txt" displays lines hwich don't contain our search word '9'.

  • grep -i: The 'grep -i' command filters output in a case-insensitive way.
  • Syntax:

    Example:

    Linux Grep Filters4

    Look at the above snapshot, command "grep -i red exm.txt" displays all lines containing 'red' whether in upper case or lower case.

  • grep -A/ grep -B/ grep -C

    grep -A command is used to display the line after the result.

    grep -B command is used to display the line before the result.

    grep -C command is used to display the line after and line before the result.

    You can use (A1, A2, A3.....)(B1, B2, B3....)(C1, C2, C3....) to display any number of lines.

  • Syntax:

    Example:

    Linux Grep Filters5

    Look at the above snapshot, command "grep -A1 yellow exm.txt" displays searched line with next succeeding line, command "grep -B1 yellow exm.txt" displays searched line with one preceding line and command "grep -C1 yellow exm.txt" displays searched line with one preceding and succeeding line.

Next TopicLinux comm




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA