One linux command per day: grep command

Keywords: Linux Ubuntu svn ssh

The grep command in Linux system is a powerful text search tool. It can use regular expressions to search text and print matching lines. The full name of grep is Global Regular Expression Print, which represents the global regular expression version, and its permission is for all users.

grep works by searching for string templates in one or more files. If the template contains spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to the standard output without affecting the content of the original file.

Grep can be used in shell scripts because grep describes the search status by returning a status value, returns 0 if the template search is successful, returns 1 if the search is unsuccessful, and returns 2 if the search file does not exist. We can use these return values to do some automated text processing.

1. Command format:

grep [option] pattern file

2. Command function:

Specific characters for filtering/searching. Regular expressions can be used in conjunction with a variety of commands and are very flexible in use.

3. Command parameters:

  • - a - text # Don't ignore binary data.
  • - A < Display Row Number > - After-context= < Display Row Number > # displays the contents after the row in addition to the column that conforms to the template style.
  • - b byte-offset indicates the number of the first character of the line before displaying the styled line.
  • - B < Display Lines > - before-context= < Display Lines > # In addition to displaying the styled line, and displaying the contents before the line.
  • - c count calculates the number of columns that match the style.
  • - C < Display Lines > - context = < Display Lines > or - < Display Lines > # displays the contents before and after the line, in addition to the line that matches the style.
  • - d < Actions > - directories = < Actions > This parameter must be used when specifying that the directory to be looked up is not a file, otherwise the grep instruction will return information and stop the action.
  • - e < Template Style > - regexp= < Template Style > # Specifies strings as styles for finding file content.
  • - E - extended-regexp # uses the style as an extended generic representation.
  • - f < Rule File > - File = < Rule File > # Specifies a rule file whose content contains one or more rule styles, allowing grep to find the content of a file that meets the requirements of the rules in a rule style per line.
  • - F - fixed-regexp # treats styles as a list of fixed strings.
  • - G - basic-regexp # uses styles as common representations.
  • - h-no-filename# does not indicate the name of the file to which the line belongs before displaying the styled line.
  • - H - with-filename # indicates the name of the file to which the line belongs before displaying the styled line.
  • - i-ignore-case # ignores the difference between case and case.
  • - l - file-with-matches # lists file names that match the specified style.
  • - L - files-without-match # lists file names whose contents do not conform to the specified style.
  • - n-line-number # indicates the column number of the line before displaying the line that conforms to the style.
  • - q - quiet or - silent # does not display any information.
  • - r - recursive # This parameter works the same as the specified "- d recurse" parameter.
  • - s-no-messages# does not display error messages.
  • - v-revert-match# displays all rows that do not contain matched text.
  • - V - version # displays version information.
  • - w-word-regexp# displays only full-character columns.
  • - x-line-regexp# displays only columns that match the full column.
  • - y # The effect of this parameter is the same as that of the specified "-i" parameter.

4. Regular expressions:

grep's regular expression:

  • ^# The beginning of the anchor line is as follows:'^ grep'matches all rows starting with grep.
  • The end of the anchored line is as follows:'grep $'matches all rows ending in grep.
  • Matches a character that is not a newline character, such as:'gr.p'matches gr, followed by an arbitrary character, and then P. ___________
    • # Match zero or more previous characters such as'* grep'to match all or more spaces followed by grep's rows.
  • * # is used together to represent any character.
  • []# matches a character within a specified range, such as'[Gg]rep'matches Grep and grep.
  • [^]# matches a character that is not in the specified range, such as'[^ A-FH-Z]rep'matches that do not contain the beginning of a letter of A-R and T-Z, followed by the line of rep.
  • \ (. ) Mark matching characters, such as' lov e', and lov e is marked 1.
  • \ The beginning of the anchored word, such as:' grep'matches the line containing the word beginning with grep.
  • \># The end of the anchored word, such as'grep >'matches the line containing the word ending in grep.
  • x{m} # Repeat the character x, m times, such as:'0 {5} matches rows containing 5 o.
  • x{m,}# Repeat the character x at least m times, e.g.'o{5,}'matches rows with at least five o's.
  • x{m,n}# Repeat character x, at least m times, not more than n times, such as:'o{5,10}'matching lines of 5-10 o.
  • \ w# matches text and numeric characters, that is, [A-Za-z0-9], such as `G w*p'matches with G followed by zero or more text or numeric characters, and then P.
  • \ The inverted form of W \ w matches one or more non-word characters, such as period, period, etc.
  • \ b # word locker, such as' b grep b'matches grep only.

POSIX characters:

In order to keep one to one character encoding 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]. They need to be placed in [] numbers to become regular expressions, such as [A-Za-z0-9] or [: alnum:]. Except fgrep, grep under linux supports POSIX character classes.

[: alnum:] Text numeric characters
[: alpha:] Text characters
[: digit:] Numeric characters
[: graph:] Non-empty characters (non-spaces, control characters)
[: lower:] lowercase characters
[: cntrl:] Control Character
[: print:] Non-empty characters (including spaces)
[: punct:] punctuation
[: space:]# All blank characters (new lines, spaces, tabs)
[: upper:] uppercase characters
[: xdigit:] Hexadecimal digits (0-9, a-f, A-F)

5. Use examples:

Example 1: Find the specified process
Order:

ps -ef|grep svn

Output:

[root@localhost ~]# ps -ef|grep svn
root 4943   1      0  Dec05 ?   00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838  0 19:53 pts/0    00:00:00 grep svn
[root@localhost ~]#

Explain:

The first record is the process of finding; the second result is the grep process itself, not the real process.

Example 2: Find the number of specified processes
Order:

ps -ef|grep svn -c
ps -ef|grep -c svn

Output:

[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn 
2
[root@localhost ~]#

Explain:

Example 3: Read keywords from files for search
Order:

cat test.txt | grep -f test2.txt

Output:

[root@localhost test]# cat test.txt 
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt 
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#

Explain:

Output test.txt file contains the content lines of keywords read from the test2.txt file

Example 3: Read keywords from files for search and display line numbers
Order:

cat test.txt | grep -nf test2.txt

Output:

[root@localhost test]# cat test.txt 
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt 
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#

Explain:

Output test.txt file contains the content lines of keywords read from the test2.txt file and displays the line number of each line.

Example 5: Find keywords from files
Order:

grep 'linux' test.txt

Output:

[root@localhost test]# grep 'linux' test.txt 
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt 
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#

Explain:

Example 6: Find keywords from multiple files
Order:

grep 'linux' test.txt test2.txt

Output:

[root@localhost test]# grep -n 'linux' test.txt test2.txt 
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt 
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#

Explain:

When multiple files are output, the name of the file will be output at the front of the line and the ":" will be added as an identifier when the query information content line is output.

Example 7: grep does not display its own process
Order:

ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"

Output:

[root@localhost test]# ps aux|grep ssh
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd
root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 
root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd
root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd
root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0

Explain:

Example 8: Find lines that start with u
Order:

cat test.txt |grep ^u

Output:

[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#

Explain:

Example 9: Output line content that does not start with u
Order:

cat test.txt |grep ^[^u]

Output:

[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#

Explain:

Example 10: Output line content ending with hat
Order:

cat test.txt |grep hat$

Output:

[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#

Explain:

Example 11:
Order:
Output:

[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
[root@localhost test]#

Explain:

Example 12: Display lines containing ed or at characters
Order:

cat test.txt |grep -E "ed|at"

Output:

[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#

Explain:

Example 13: Displays all rows of strings with at least seven consecutive lowercase characters per string in a file ending in. txt under the current directory
Order:

grep '[a-z]\{7\}' *.txt

Output:

[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#

Explain:

Posted by sepodati on Thu, 28 Mar 2019 06:30:31 -0700