Common Linux commands () and common linux commands

Source: Internet
Author: User
Tags character classes control characters

Common Linux commands () and common linux commands

In Linux, The grep command is a powerful text search tool that uses regular expressions to search for text and print Matching lines. Grep stands for Global Regular Expression Print, which indicates the Global Regular Expression version. Its permission is granted to all users.


Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the standard output without affecting the content of the original file.


Grep can be used in shell scripts because grep returns a status value to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.

1.Command Format:

Grep [option] pattern file


2.Command function:

Specific Characters Used for filtering/searching. Regular Expressions can be used in combination with multiple commands.


3.Command parameters:

-A -- text # Do not ignore binary data.

-A <display number of rows> -- after-context = <display number of rows> # In addition to displaying the column that conforms to the template style, the content after the row is displayed.

-B -- byte-offset # indicates the number of the first character of the line before the line that conforms to the style is displayed.

-B <display number of rows> -- before-context = <display number of rows> # In addition to the line that conforms to the style, and the content before the line is displayed.

-C -- count # calculate the number of columns that match the style.

-C <display number of rows> -- context = <display number of rows> or-<display number of rows> # In addition to the line that conforms to the style, and the content before and after the line is displayed.

-D <action> -- directories = <action> # this parameter is required when you specify a directory rather than a file to be searched. Otherwise, the grep command returns information and stops the action.

-E <template style> -- regexp = <template style> # specify a string as the style for searching the file content.

-E -- extended-regexp # Use the style as an extended normal notation.

-F <rule file> -- file = <rule file> # specifies the rule file, which contains one or more rule styles, so that grep can find the file content that meets the rule conditions, the format is one rule style per line.

-F -- fixed-regexp # lists the styles as fixed strings.

-G -- basic-regexp # use styles as normal notation.

-H -- no-filename # The name of the file to which the row conforms to the style is not displayed.

-H -- with-filename # indicates the name of the file to which the row belongs before the row that conforms to the style is displayed.

-I -- ignore-case # ignore case sensitivity differences.

-L -- file-with-matches # lists the names of objects whose contents match the specified style.

-L -- files-without-match # lists the names of files whose contents do not conform to the specified style.

-N -- line-number # indicates the number of columns of the row before the row that conforms to the style is displayed.

-Q -- quiet or -- silent # No information is displayed.

-R -- recursive # the effect of this parameter is the same as that of the specified "-d recurse" parameter.

-S -- no-messages # the error message is not displayed.

-V -- revert-match # displays all rows that do not contain matched text.

-V -- version # displays the version information.

-W -- word-regexp # Only columns with full-text matching are displayed.

-X -- line-regexp # Only displays columns that match the full column.

-Y # the effect of this parameter is the same as that of the specified "-I" parameter.


4.RulesExpression:

Grep rule expression:

^ # Start of the anchor row: '^ grep' matches all rows starting with grep.

$ # End of the anchor row, for example, 'grep $ 'matches all rows ending with grep.

. # Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character, followed by p.

* # Match zero or multiple previous characters, for example, '* grep' matches all rows followed by one or more spaces.

. * # Represents any character together.

[] # Match a character in a specified range, for example, '[Gg] rep' matches Grep and grep.

[^] # Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by rep.

\ (.. \) # MARK matching characters, such as '\ (love \)', and love is marked as 1.

\ <# Specifies the start of a word, for example, '\ <grep' matches a row that contains a word starting with grep.

\># Specify the end of a word, such as 'grep \>.

X \ {m \} # repeated characters x, m times, for example, '0 \ {5 \} 'match rows containing 5 o.

X \ {m, \} # repeated characters x, at least m times, such as: 'O \ {5, \} 'matching rows with at least 5 o.

X \ {m, n \} # repeated character x, at least m times, no more than n times, for example, 'O \ {5, 10 \} 'matches rows of 5-10 o.

\ W # match text and numeric characters, that is, [A-Za-z0-9], such as: 'G \ w * P' match with G followed by zero or more characters or numbers, then p.

The inverse form of \ W # \ w. It matches one or more non-word characters, such as periods and periods.

\ B # The word lock. For example, '\ bgrep \ B' matches only grep.


POSIX characters:

To ensure one character encoding in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [: alnum:] is another way of writing [A-Za-z0-9. Put them in the [] sign to become A regular expression, such as [A-Za-z0-9] or [[: alnum:]. In linux, grep supports POSIX character classes except fgrep.

[: Alnum:] # numbers and characters

[: Alpha:] # text characters

[: Digit:] # numeric characters

[: Graph:] # non-empty characters (non-space, control characters)

[: Lower:] # lowercase characters

[: Cntrl:] # control characters

[: Print:] # non-empty characters (including spaces)

[: Punct:] # punctuation marks

[: Space:] # All blank characters (new lines, spaces, and tabs)

[: Upper:] # uppercase characters

[: Xdigit:] # hexadecimal number (0-9, a-f, A-F)


5.Instance used:

Instance 1:Search for a specified process

Command: ps-ef | grep svn

Note: The first record is the process to be searched; the second record is the grep process itself, not the process to be searched.

[root@localhost ~]# ps -ef|grep svnroot 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 ~]#

Instance 2: Query the number of Specified Processes

Command:

Ps-ef | grep svn-c

Ps-ef | grep-c svn

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

Example 3:Read keywords from files for search

Command: cat test.txt | grep-f test2.txt

Note: The output test.txt file contains the content line of the keywords read from the test2.txt file.

[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -f test2.txthnlinuxubuntu linuxRedhatlinuxmint[root@localhost test]#

Example 4:Read keywords from files for search and display row numbers

Command: cat test.txt | grep-nf test2.txt

Note: The output test.txt file contains the content line of the keywords read from the test2.txt file, and displays the row number of each row.

[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -nf test2.txt1:hnlinux4:ubuntu linux6:Redhat7:linuxmint[root@localhost test]#

Example 5:Search for keywords from files

Command: grep 'linux 'test.txt

[root@localhost test]# grep 'linux' test.txt hnlinuxubuntu linuxlinuxmint[root@localhost test]# grep -n 'linux' test.txt 1:hnlinux4:ubuntu linux7:linuxmint[root@localhost test]#


Instance 6:Search for keywords from multiple files

Command: grep 'linux 'test.txt test2.txt

Note: When multiple files are output, the file name is output at the beginning of the row and ":" is added as the identifier.

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

Example 7:Grep does not display its own processes

Command:

Ps aux | grep \ [s] sh

Ps aux | grep ssh | grep-v "grep"

[root@localhost test]# ps aux|grep sshroot   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshdroot  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]shroot   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshdroot  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/sshdroot  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0

Instance 8:Find the row content starting with u

Command: cat test.txt | grep ^ u

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

Instance 9:Output line content not starting with u

Command: cat test.txt | grep ^ [^ u]

[root@localhost test]# cat test.txt |grep ^[^u]hnlinuxpeida.cnblogs.comredhatRedhatlinuxmint[root@localhost test]#

Instance 10:Output The Line Content ending with hat

Command: cat test.txt | grep hat $

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

Instance 11:Display content lines containing ed or at characters

Command: cat test.txt | grep-E "ed |"

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

Instance 12:Displays all rows in the file ending with .txt in the current directory that contain at least seven consecutive lowercase characters in each string.

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

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

Address: http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.