One linux Command (39) every day: grep command

Source: Internet
Author: User


One linux Command every day (39): grep command link: One linux Command every day (1): ls command http://www.bkjia.com/ OS /210210/163049.html#linuxlinuxcommand every day (2): cd command quit (3 ): pwd command Export (4): mkdir command http://www.bkjia.com/ OS /201210/1620.3.html#a linuxcommand every day (5): rm command Export (6): rmdir command http://www.bkjia.com/ OS /201210/164017. Html; one linux Command every day (7): mv command running (8): cp command http://www.bkjia.com/ OS /201210/163164.html#a linuxcommand every day (9): touch command running (10): cat command running (11 ): nl command http://www.bkjia.com/ OS /201211/165990.htmla linuxcommand every day (12): more command http://www.bkjia.com/ OS /201211/165994.htmllinuxcommand every day (13): Less command restart (14): head command http://www.bkjia.com/ OS /201211/166191.htmllinuxcommand every day (15): tail command restart (16): which command restart (17): whereis command restart (18 ): locate command http://www.bkjia.com/ OS /201211/168895.htmla linuxcommand every day (19): find command overview h Ttp: // timeout (20): exechttp of the find command: // www.bkjia.com/ OS /201211/168901.htmllinuxcommand every day (21): xargshttp of the find command: // timeout (22 ): detailed description of the parameters of the find command: http://www.bkjia.com/ OS /201211/168912.htmla linuxcommand every day (23): Linux directory structure (24): Linux file type and extension (2 5): linux File Attribute explanation example (26): Use SecureCRT to upload and download files. Example (27): linux chmod command example (28): tar command http://www.bkjia.com/ OS /201212/172.161.html A linuxcommand every day (29 ): chgrp command http://www.bkjia.com/ OS /201212/172983.htmla linuxcommand every day (30): chown command http://www.bkjia.com/ OS /201212/173239.htmlone per day Linux Command (31):/etc/group file detailed explanation http://www.bkjia.com/ OS /201212/174429.htmlevery day a linuxcommand (32): gzip command running (33): df command running (34): du command running (35 ): ln command http://www.bkjia.com/ OS /201212/174993.htmla linuxlinuxcommand every day (36): diff command http://www.bkjia.com/ OS /201212/176333.htmllinuxcommand every day (37): da Te command http://www.bkjia.com/ OS /201212/176335.htmllinuxcommand every day (38): cal command http://www.bkjia.com/ OS /201212/176337.html in Linux system grep command is a powerful text search tool, it can use regular expressions to search for text, print out the matching rows. 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. -A <show the number of columns> -- after-context = <show the number of columns> # In addition to displaying the column that conforms to the template style, and displaying the content after the column. -B -- byte-offset # indicates the first character Number of the column before displaying the column that conforms to the style. -B <display columns> -- before-context = <display columns> # In addition to displaying the columns that conform to the style, and display the content before the column. -C -- count # calculate the number of columns that match the style. -C <display columns> -- context = <display columns> or-<display columns> # In addition to displaying the columns that conform to the style, and display the content before and after the column. -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 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. \ <# 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 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] #
 

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.