One linux command (39) every day: grep command

Source: Internet
Author: User
Tags character classes control characters touch command
Every day, a linux command (39): grep command link: A linux command (1): ls command (2): cd command http://www.2cto.com/ OS /201210/163050.html#a linux command every day ..
One linux command every day (39): grep command link: One linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html ; One linux command every day (3): pwd command http://www.2cto.com/os/201210/163462.html ; One linux command every day (4): mkdir command http://www.2cto.com/os/201210/163463.html ; One linux command every day (5): rm command http://www.2cto.com/os/201210/163662.html ; One linux command (6) every day: rmdir command http://www.2cto.com/os/201210/164017.html ; One linux command (7) every day: mv command http://www.2cto.com/os/201210/164247.html ; One linux command every day (8): cp command http://www.2cto.com/os/201210/164254.html ; One linux command every day (9): touch Command http://www.2cto.com/os/201211/165699.html ; One linux command every day (10): cat command http://www.2cto.com/os/201211/165989.html ; One linux command every day (11): nl command http://www.2cto.com/os/201211/165990.html One linux command every day (12): more command http://www.2cto.com/os/201211/165994.html One linux command every day (13): less command http://www.2cto.com/os/201211/165998.html One linux command every day (14): head Command http://www.2cto.com/os/201211/166191.html One linux command every day (15): tail command http://www.2cto.com/os/201211/168702.html One linux command every day (16): which command http://www.2cto.com/os/201211/168890.html A linux command (17) every day: whereis command http://www.2cto.com/os/201211/168893.html One linux command (18) every day: locate command http://www.2cto.com/os/201211/168895.html One linux command every day (19): find command overview http://www.2cto.com/os/201211/168897.html One linux command every day (20): find command exec http://www.2cto.com/os/201211/168901.html One linux command (21) every day: find command xargs http://www.2cto.com/os/201211/168903.html A linux command (22) every day: detailed description of the parameters of the find Command http://www.2cto.com/os/201211/168912.html A linux command (23) every day: Linux directory structure http://www.2cto.com/os/201211/170430.html One linux command every day (24): Linux file type and extension http://www.2cto.com/os/201211/170431.html One linux command every day (25): Explanation of linux file attributes http://www.2cto.com/os/201211/170434.html One linux command every day (26): use SecureCRT to upload and download files http://www.2cto.com/os/201211/172022.html One linux command every day (27): linux chmod command http://www.2cto.com/os/201211/172028.html One linux command every day (28): tar command http://www.2cto.com/os/201212/172641.html One linux command (29) every day: chgrp command http://www.2cto.com/os/201212/172983.html One linux command every day (30): chown command http://www.2cto.com/os/201212/173239.html A linux command (31) every day:/etc/group file details http://www.2cto.com/os/201212/174429.html One linux command (32) every day: gzip command http://www.2cto.com/os/201212/174431.html One linux command every day (33): df command http://www.2cto.com/os/201212/174434.html One linux command every day (34): du command http://www.2cto.com/os/201212/174701.html One linux command (35) every day: ln command http://www.2cto.com/os/201212/174993.html One linux command (36) every day: diff command http://www.2cto.com/os/201212/176333.html One linux command (37) every day: date Command http://www.2cto.com/os/201212/176335.html One linux command (38) every day: cal command http://www.2cto.com/os/201212/176337.html 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. Www.2cto.com grep works in this way. it searches for a string template 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: www.2cto.com grep [option] pattern file2. command function: used to filter/search specific characters. Regular expressions can be used in combination with multiple commands. 3. command parameters:-a -- text # Do not ignore binary data.- <显示列数> -- After-context = <显示列数> # In addition to displaying the column that conforms to the template style, the content after the column is displayed. -B -- byte-offset # indicates the first character number of the column before displaying the column that conforms to the style. -B <显示列数> -- Before-context = <显示列数> # In addition to displaying the column that conforms to the style, and displaying the content before the column. -C -- count # calculate the number of columns that match the style. -C <显示列数> -- Context = <显示列数> Or- <显示列数> # In addition to displaying the column that meets the style, and displaying the content before and after the column. -D <动作> -- Directories = <动作> # This parameter must be used when you specify a directory rather than a file to be searched. Otherwise, the grep command returns information and stops the operation. -E <范本样式> -- Regexp = <范本样式> # Specify a string as the style for searching the file content. -E -- extended-regexp # use the style as an extended normal notation. -F <规则文件> -- File = <规则文件> # Specify the rule file. the content of the rule file contains one or more rule styles. let grep search for the file content that meets the rule conditions. The format is one rule style for each column. -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 column conforms to the style is not displayed. -H -- with-filename # indicates the file name of the column before displaying the column that conforms to the style. -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 in a style column before it 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. rule expression: the rule expression of grep: ^ # start of the anchor row, for example, '^ 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. \ <# Specify the start of a word, for example :'\ # Pin the end of a word, for example, 'grep \> 'matches the row containing the word ending with 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 character: in order to keep one character encoding in different countries, POSIX (The Portable Operating System Interface) adds a special character class, 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. Www.2cto.com [: alnum:] # text and numbers [: alpha:] # text character [: digit:] # Number character [: graph:] # Non-empty characters (non-space and control characters) [: lower:] # Lowercase characters [: cntrl:] # control characters [: print:] # Non-empty characters (including spaces) [: punct:] # Punctuation [: space:] # All blank characters (new lines, spaces, tabs) [: upper:] # uppercase characters [: xdigit:] # hexadecimal number (0-9, a-f, A-F) 5. use instance: instance 1: Find the specified process Command: ps-ef | grep svn output: [root @ localhost ~] # Ps-ef | grep svnroot 4943 1 0 Dec05? 00:00:00 svnserve-d-r/opt/svndata/grape/root 16867 16838 0 00:00:00 pts/0 grep svn [root @ localhost ~] # Note: The first record is the process to be searched; the second record is the grep process itself, not the process to be searched. Instance 2: run the following command to query the number of specified processes: ps-ef | grep svn-cps-ef | grep-c svn output: [root @ localhost ~] # Ps-ef | grep svn-c2 [root @ localhost ~] # Ps-ef | grep-c svn 2 [root @ localhost ~] # Note: www.2cto.com instance 3: Read the keyword from the file and search for the command: cat test.txt | grep-f test2.txt output: [root @ localhost test] # cat test.txt hnlinuxpeida. cnblogs. comubuntu linuxredhatRedhatlinuxmint [root @ localhost test] # cat test2.txt linuxRedhat [root @ localhost test] # cat test.txt | grep-f test2.txthnlinuxubuntu linuxRedhatlinuxmint [root @ localhost test] # note: example 3: reading a keyword from a file Command: cat test.txt | grep-nf test2.txt output: [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] # 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. Example 5: search for keywords from the file command: grep 'Linux 'test. Txt output: [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] # Note: instance 6: Find the keyword command from multiple files: grep 'Linux 'test.txt test2.txt output: [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] # gr Ep 'Linux 'test.txt test2.txt test.txt: hnlinuxtest.txt: ubuntu linuxtest.txt: linuxminttest2.txt: linux [root @ localhost test] # Note: when multiple files are used, when the queried information is output, the file name will be output at the beginning of the row and ":" will be added as the identifier instance 7: grep does not display its own process command: ps aux | grep \ [s] shps aux | grep ssh | grep-v "grep" output: [root @ localhost test] # ps aux | grep sshroot 2720 0.0 0.0 62656 1212? Ss Nov02/usr/sbin/sshdroot 16834 0.0 0.0 88088 3288? Ss sshd: root @ pts/0 root 16901 0.0 0.0 61180 764 pts/0 S + 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/usr/sbin/sshdroot 16834 0.0 0.0 88088 3288? Ss sshd: root @ pts/0 [root @ localhost test] # ps aux | grep ssh | grep-v "grep" root 2720 0.0 0.0 62656? Ss Nov02/usr/sbin/sshdroot 16834 0.0 0.0 88088 3288? Ss sshd: root @ pts/0 Note: instance 8: find the line content command starting with u: cat test.txt | grep ^ u output: [root @ localhost test] # cat test.txt | grep ^ uubuntuubuntu linux [root @ localhost test] # Note: instance 9: The Command output line content not starting with u: cat test.txt | grep ^ [^ u] output: [root @ localhost test] # cat test.txt | grep ^ [^ u] hnlinuxpeida. cnblogs. comredhatRedhatlinuxmint [root @ localhost test] # Note: instance 10: output the line content command ending with hat: cat test.txt | grep hat $ output: [root @ localhost test] # cat test.txt | grep hat $ redhatRedhat [root @ localhost test] # Note: instance 11: Command: 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] # Description: www.2cto.com instance 12: command line for displaying content containing ed or at characters: 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" redhatRedhat [root @ localhost test] # description: instance 13: displays all the strings that contain at least seven consecutive lowercase characters in the files ending with .txt in the current directory. the command line is grep '[a-z] \ {7 \}'*. txt output: [root @ localhost test] # grep '[a-z] \ {7 \}' * .txttest.txt: hnlinuxtest.txt: peida.cnblogs.comtest.txt: linuxmint [root @ localhost test] #
 
Related Article

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.