Starting from a Linux grep command, linuxgrep

Source: Internet
Author: User

Starting from a Linux grep command, linuxgrep

During Development today, I saw my colleagues using such a linux Command.

grep 'class YourClass' -rwi * |grep -v svn

 

Think of the grep command, several parameters.

  

-R explicitly requires that you search for sub-directories (ignore sub-directories as-d skip)-I searches case-insensitive. By default, case sensitive-w only matches the entire word, not a part of the string (for example, matching 'Magic ', rather than 'magical').-v displays all rows that do not contain matching text

 

Mark

The details are as follows:

 

1. The grep command in Linux is a powerful text search tool. It can use 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. 2. Format: grep [options] 3. Main Parameter: [options] Main Parameter:-c: only counts matching rows are output. -I: It is case-insensitive (only applicable to single characters ). -H: When querying multiple files, the file name is not displayed. -L: When querying multiple files, only names containing matching characters are output. -N: displays matching rows and row numbers. -S: the error message that does not exist or does not match the text is not displayed. -V: displays all rows that do not contain matched text. The main parameter of the regular expression pattern is \: Ignore the original meaning of special characters in the regular expression. ^: Match the start line of the regular expression. $: Matches the end row of the regular expression. \ <: Starts from the row that matches the regular expression. \>: Ends with the row that matches the regular expression. []: A single character. For example, [A] indicates that A meets the requirements. [-]: Range, such as [A-Z], that is, A, B, C Until Z all meet the requirements ..: All single characters. *: It can contain 0 characters. 4. The grep command uses a simple instance $ grep 'test' d * to display all lines containing test in files starting with d. $ Grep 'test' aa bb cc is displayed in the aa, bb, and cc files that match the test row. $ Grep '[a-z] \ {5 \}' aa displays all rows of a string containing at least five consecutive lowercase characters. $ Grep 'W \ (es \) t. * \ 1' aa if the west is matched, the es is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another es (\ 1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape it by using the "\" character. You can directly write it as 'W (es) t. * \ 1. 5. the grep command uses a complex instance to search for a file with a character string 'Magic 'in the'/usr/src/Linux/doc' directory: $ grep magic/usr/src/Linux/Doc/* sysrq.txt: * How do I enable the magic SysRQ key? Sysrq.txt: * How do I use the magic SysRQ key? The 'sysrp.txt 'file contains this string. The SysRQ function is discussed. By default, 'grep' only searches for the current directory. If there are many subdirectories in this directory, 'grep' will be listed in the following form: grep: sound: Is a directory, which may make 'grep' output hard to read. There are two solutions: Search for subdirectories: grep-r or ignore subdirectories: grep-d skip if there are many outputs, you can use the pipeline to transfer it to 'less '. Read: $ grep magic/usr/src/Linux/Documentation/* | less, so that you can read it more conveniently. Note that you must provide a file filtering method (* for searching all files *). If you forget, 'grep' will wait until the program is interrupted. If this happens, press <CTRL c> and try again. The following are some interesting command line parameters: grep-I pattern files: case-insensitive search. By default, it is case sensitive. grep-l pattern files: only names of matched files are listed. grep-L pattern files: names of unmatched files are listed. grep-w pattern files: only the entire word is matched, instead of a part of the string (such as matching 'Magic ', rather than 'magical'), grep-C number pattern files: The matched context displays the rows of [number], grep pattern1 | pattern2 files: show the rows matching pattern1 or pattern2, grep pattern1 files | grep pattern2: displays the rows matching both pattern1 and pattern2. Grep-n pattern files displays the row number information grep-c pattern files to find the total number of rows. Here, some special symbols used for searching: \ <and \> mark the start and end of the word respectively. For example, grep man * matches 'Batman ', 'manic', and 'man '. grep' \ <man '* matches 'manic' and 'man ', but not 'Batman ', grep' \ <man \>' only matches 'man ', not other strings such as 'Batman' or 'manic. '^': Indicates the beginning of a matched string, '$': indicates the end of a matched string. usage of the Grep command 1. Parameter:-I: case-insensitive-c: print the number of matched rows-l: Search for rows containing matching items from multiple files-v: Find rows without matching items-n: print the rows and Row labels containing matching Items 2 and RE (regular expression) \ ignore the original meaning of special characters in the regular expression ^ match the starting line of the regular expression $ match the ending line of the regular expression \ <starting from the line matching the regular expression \> match the Regular Expression end of line [] a single character; for example, [A] that A meets the requirements [-] range; for example, [A-Z] that A, B, C Until Z all meet the requirements. all single characters * All characters. The length can be 03. Example # ps-ef | grep in. telnetdroot 19955 181 0 13:43:53? 0: 00 in. telnetd # more size.txt size file content limit # more size.txt | grep '[a-B]' range; for example, [A-Z] is A, B, both C and Z meet the requirements of b124230b034325a081016a022021a061048b103303a0131_b044525 # more size.txt | grep '[a-B]' * then B103303BADc2345 # more size.txt | grep 'B' single character; for example, [A] indicates that A meets the requirements of b124230b034325b103303b044525 # more size.txt | grep '[bB] 'prop # grep 'root'/etc/grouproot: 0: rootbin: 2: root, bin, daemonsys: 3: root, bin, sys, admadm: 4: root, adm, daemonuucp: 5: root, uucpmail: 6: roottty: 7: root, tty, admlp: 8: root, lp, admnuucp: 9: root, nuucpdaemon: 12: root, daemon # grep '^ root'/etc/group Match the starting line of the regular expression root: 0: root # grep 'ucp'/etc/groupuucp: 5: root, uucpnuucp: 9: root, nuucp # grep '\ <uucp'/etc/groupuucp: 5: root, uucp # grep 'root $ '/etc/group matches the end row of the regular expression. root: 0: rootmail: 6: root # more size.txt | grep-I 'b1 .. * 3 '-I: case-insensitive b124230b103303B103303 # more size.txt | grep-iv 'b1 .. * 3 '-v: Search for rows that do not contain a match # m Ore size.txt | grep-in 'b1 .. * 3 '1: b1242309: b10330315: B103303 # grep '$'/etc/init. d/nfs. server | wc-l128 # grep '\ $'/etc/init. d/nfs. server | wc-l ignore the original meaning of special characters in regular expressions 15 # grep '\ $'/etc/init. d/nfs. servercase "$1" in>/tmp/sharetab. $ ["x $ fstype "! = Xnfs] & echo "$ path \ t $ res \ t $ fstype \ t $ opts \ t $ desc">/tmp/sharetab. $/usr/bin/touch-r/etc/dfs/sharetab/tmp/sharetab. $/usr/bin/mv-f/tmp/sharetab. $/etc/dfs/sharetabif [-f/etc/dfs/dfstab] &/usr/bin/egrep-v '^ [] * (# | $) 'If [$ startnfsd-eq 0-a-f/etc/rmmount. conf] & if [$ startnfsd-ne 0]; thenelif [! -N "$ _ INIT_RUN_LEVEL"]; thenwhile [$ wtime-gt 0]; dowtime = 'expr $ wtime-1 'if [$ wtime-eq 0]; thenecho "Usage: $0 {start | stop} "# more size.txt the test filetheir are filesThe end # grep 'the 'size.txt the test filetheir are files # grep' \ <the 'size.txt the test filetheir are files # grep 'the \> 'size.txt the test file # grep' \ <the \> 'size.txt the test file # grep' \ <[Tt] he \> 'size.txt the test fil E ============================================== =====================================1, A multi-purpose text search tool that uses regular expressions. this php? Name = % C3 % FC % C1 % EE "onclick =" tagshow (event) "class =" t_tag "> is a php? Name = % C3 % FC % C1 % EE "onclick =" tagshow (event) "class =" t_tag "> command/filter: g/re/p -- global-regular expression-print. basic Format: grep pattern [file...] (1) grep search string [filename] (2) grep regular expression [filename] searches for all occurrences of pattern in the file. pattern can be either a string to be searched, it can also be a regular expression. note: It is best to use double quotation marks when entering the string to be searched. When using regular expressions in pattern matching, use single quotation marks 2, grep option-c only outputs the Count of matched rows-I is case insensitive (for single character) -n: show the matched row number-v: do not show the line that does not contain the matched text, so the line-s does not show the error message-E. Use the extended regular expression. For more options, see man grep3, common Grep instance (1) query grep "sort "*. doc # see matching of file names (2) Row matching: Output count of matched rows grep-c "48" data.doc # number of lines containing 48 characters in the output document (3) show matched rows and lines grep-n "48" data.doc # Show All rows and lines that match 48 (4) show unmatched rows grep-vn "48" data.doc # output all rows that do not contain 48 (4) show unmatched rows grep-vn "48" data.doc # output all rows that do not contain 48 (5) case Sensitive grep-I "AB" data.doc # output all lines containing the AB or AB strings. 4. Apply the regular expression (1) application of the regular expression (note: it is best to enclose the Regular Expression in single quotes) grep '[239]. 'data.doc # output all rows that contain numbers starting with 2, 3, and 9 (2) do not match the test grep' ^ [^ 48]' Data.doc # do not match the rows whose first line is 48 (3) use the extended mode to match grep-E '2017 | 100' data.doc (4 )... this requires constant application and summarization in practice to master regular expressions. 5. Use a class name that can match internationally: [[: upper:] [A-Z] [[: lower:] [a-z] [[: digit:] [0-9] [[: alnum:] [0-9a-zA-Z] [[: space:] space or tab [[: alpha:] [a-zA-Z] (1) Use grep '5 [[: upper:] [[: upper:] 'data.doc # query the rows whose names start with 5 and end with two uppercase letters

 

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.