grep command of Linux

grep (global search regular expression(RE) and print out the line) is a powerful text search tool. It can use regular expressions to search text and print out matching lines. Specific characters used for filtering / searching. Regular expressions can be used. They can be used with a variety of commands and are very flexible.

Command format

grep [options] [pattern] [file ...]

Command function

Specific characters for filtering / searching

options

  • -a --text # do not ignore binary data.
  • -A < number of display lines > -- after context = < number of display lines > # in addition to the line that conforms to the template style, it also displays the content after the line.
  • -B -- byte offset # in addition to the line that conforms to the template style, and displays the content before the line.
  • -B < number of lines to display > -- before context = < number of lines to display > # in addition to the line matching the style, the content before the line is displayed.
  • -c --count # calculates the number of columns that conform to the template style.
  • -C < display rows > -- context = < display rows > or - < display rows > # in addition to the column that conforms to the template style, the contents before and after the column are displayed.
  • -D < action > -- directories = < action > # this parameter must be used when you specify that you want to find a directory rather than a file, otherwise the grep command will report information and stop the action.
  • -E < template style > -- regexp = < template style > # specifies the string as the template style to find the contents of the file.
  • -E -- extended regexp # uses the template style as an extended common representation, which means that extended regular expressions can be used.
  • -F < template file > -- file = < rule file > # specifies a template file whose content has one or more template styles. Let grep find the file content that meets the template conditions. The format is the template style of each column.
  • -F -- Fixed regexp # treats the template style as a list of fixed strings.
  • -G -- Basic regexp # treats the template style as a common notation.
  • -H -- no filename # before the column conforming to the template style is displayed, the file name to which the column belongs is not marked.
  • -H -- with filename # indicates the file name of the column before displaying the column that conforms to the template style.
  • -I -- ignore case # ignores the case difference of characters.
  • -L -- file with matches # lists the file names whose contents conform to the specified template style.
  • -L -- files without match # lists the names of files whose contents do not conform to the specified template style.
  • -N -- line number # indicates the number of the column before displaying the column that conforms to the template style.
  • -P -- Perl regexp # pattern is a Perl regular expression
  • -q --quiet or -- silent # does not display any information.
  • -R / - R -- recursive # this parameter has the same effect as specifying the "- d recurse" parameter.
  • -S -- no messages # does not display error messages.
  • -V -- reverse match # reverse lookup.
  • -V --version # displays version information.
  • -W -- word regexp # only displays columns that match the whole word.
  • -X -- line regexp # only displays the columns that match all the columns.
  • -y # this parameter has the same effect as "- i".
  • -o # only the matched part of the file is output.
  • -M < num > -- Max count = < num > # stop searching after finding the result of num row, which is used to limit the number of matching rows

pattern

  • ^# anchor the beginning of the line, such as' ^ grep 'matches all lines beginning with grep.
  • $# anchor the end of the line, such as: 'grep $' matches all lines ending in grep.
  • . # matches a non newline character, such as: 'gr.p' matches GR followed by an arbitrary character, followed by P.
  • *# matches zero or more previous characters, such as' * grep 'matches all one or more spaces followed by the line of grep.
  • . * # together represents any character.
  • [] # matches characters within a specified range, such as' Ggrep 'matches grep and grep.
  • [^] # matches a character that is not within the specified range, such as' ^ A-FH-Zrep 'matches a line that does not contain a letter beginning with A-R and T-Z followed by rep.
  • \(.. \) # mark matching characters, such as' (love) ', love is marked as 1.
  • \< # anchor the beginning of a word, e.g. '\ < grep' matches a line containing a word beginning with grep.
  • \># anchor the end of the word, such as' grep > 'to match the line containing the word ending in grep.
  • x\{m \} # repeat the character x, m times, for example: '0 {5}' matches the line containing 5 o's.
  • x\{m, \} # repeat the character x at least m times. For example, 'o{5,}' matches a line with at least 5 o's.
  • x\{m,n \} # repeat the character x at least m times and no more than n times. For example, 'o{5,10}' matches 5-10 o lines.
  • \W # matches literal and numeric characters, i.e. A-Za-z0-9. For example, 'G\w*p' matches G followed by zero or more literal or numeric characters, followed by P.
  • \W # \ the inverse form of W, which matches one or more non word characters, such as period, etc.
  • \b # word lock, such as' \ bgrep\b 'only matches grep.

POSIX character

In order to keep consistent character coding in different countries, POSIX(The Portable Operating System Interface) adds special character classes, such as alnum: which is another way of writing A-Za-z0-9. You need to put them in the [] sign to become regular expressions, such as A- Za-z0-9 or [: alnum:]. grep under linux supports POSIX character classes except fgrep.

  • : alnum: # alphanumeric characters
  • : alpha: # text character
  • : digit: # numeric character
  • : graph: # non empty characters (non spaces, control characters)
  • : lower: # lowercase characters
  • : cntrl: # control character
  • : print: # non empty characters (including spaces)
  • : punct: # punctuation
  • : Space: # all white space characters (new lines, spaces, tabs)
  • : upper: # uppercase characters
  • : xdigit: # hexadecimal digit (0-9, A-F, A-F)

Find the specified process

> ps -ef | grep java
root     16934     1  0 Feb25 ?        00:12:23 java -jar demo.jar
root      6891  2151  0 21:42 pts/2    00:00:00 grep --color=auto java

The first record is the process found; The second result is that the grep process itself is not the process you are really looking for.

Number of lookup processes

> ps -ef | grep -c java
10

> ps -ef | grep java -c
10

Find keywords in files

> cat rumenz.txt 
rumenz.txt
one
tow
qaz
redis
linux123
linuxxxx
rumenz
123
789

> grep "linux" rumenz.txt 
linux123
linuxxxx

// -n display line number

> grep -n "linux" rumenz.txt 
6:linux123
7:linuxxxx

Read keywords from files to search

// Text to find
> cat rumenz.txt 
rumenz.txt
one
tow
qaz
redis
linux123
linuxxxx
rumenz
123
789

// Multiple keywords to find

> cat k.txt 
linux
redis

> cat rumenz.txt | grep -f k.txt
redis
linux123
linuxxxx

//set number 

> cat rumenz.txt | grep -nf k.txt 
5:redis
6:linux123
7:linuxxxx

Output rumenz Txt file contains the content line of the keyword read from the k.txt file, and -n displays the line number

Find keywords from multiple files

> grep "linux" rumenz.txt rumenz123.txt 
rumenz.txt:linux123
rumenz.txt:linuxxxx
rumenz123.txt:linux123
rumenz123.txt:linuxxxx
rumenz123.txt:linux100

In case of multiple files, when outputting the queried information content line, the file name will be output at the front of the line and added with ":" as the identifier

Find keywords from multiple files and use wildcards

// Find all files starting with rumenz in the current directory
> grep "linux" rumenz*
rumenz123.txt:linux123
rumenz123.txt:linuxxxx
rumenz123.txt:linux100
rumenz.txt:linux123
rumenz.txt:linuxxxx

// Find all in the current directory txt file

> grep "linux" *.txt
k.txt:linux
rumenz123.txt:linux123
rumenz123.txt:linuxxxx
rumenz123.txt:linux100
rumenz.txt:linux123
rumenz.txt:linuxxxx

grep does not display its own processes

> ps -ef | grep redis | grep -v grep
root     14383     1  0 Jan08 ?        01:16:03 /opt/redis-5.0.8/src/redis-server *:6379
polkitd  31977 31941  0 Jan11 ?        01:14:22 redis-server *:6379

grep -v grep does not display the grep process itself

Find the contents of the line beginning with r

> cat rumenz.txt | grep ^r
rumenz.txt
redis
rumenz

Find the contents of lines that do not begin with r

> cat rumenz.txt | grep ^[^r]
one
tow
qaz
linux123
linuxxxx
123
789

Output lines ending in 3

> cat rumenz.txt | grep 3$
linux123
123

Displays a content line containing li or 23 characters

> at rumenz.txt | grep -E "li|23"
linux123
linuxxxx
123

Find files in the specified format

Display the current directory with All lines in the file ending in txt that contain strings with at least 3 consecutive lowercase characters for each string

> grep "[a-z]\{3\}" *.txt
k.txt:linux
k.txt:redis
rumenz123.txt:rumenz.txt
rumenz123.txt:one
rumenz123.txt:tow
rumenz123.txt:qaz
rumenz123.txt:redis
rumenz123.txt:linux123
rumenz123.txt:linuxxxx
rumenz123.txt:rumenz
rumenz123.txt:linux100
rumenz.txt:rumenz.txt
rumenz.txt:one
rumenz.txt:tow
rumenz.txt:qaz
rumenz.txt:redis
rumenz.txt:linux123
rumenz.txt:linuxxxx
rumenz.txt:rumenz

Find eligible files recursively

> grep  -rl "rumenz" /root/test/
/root/test/src/sbin/patch.log
/root/test/src/doc/rumenz.txt
/root/test/src/doc/rumenz123.txt
/root/test/src/InfiniteLoop.class
/root/test/src/InfiniteLoop.java

Find rumenz Txt contains the first two lines of linux lines

> grep -B 2 "linux" rumenz.txt
qaz
redis
linux123
linuxxxx

Find rumenz Txt contains the last two lines of linux lines

> grep -A 2 "linux" rumenz.txt
linux123
linuxxxx
rumenz
123

Find rumenz Txt contains two lines before and after the linux line

> grep -C 2 "linux" rumenz.txt
qaz
redis
linux123
linuxxxx
rumenz
123

Original link: https://rumenz.com/rumenbiji/linux-grep.html

Added by NickG21 on Wed, 26 Jan 2022 02:50:50 +0200