by a Linux grep command.

Source: Internet
Author: User

At the time of development today, I saw colleagues using a Linux command like this.

grep ' class YourClass ' -rwi * | grep -V SVN

Think of a few parameters of the grep command.

  

    -R explicitly requires search for subdirectories (omit subdirectories as-D skip) -I search in case-insensitive. The default case is case-W  matches only the entire word, not part of the string (such as matching ' magic ' instead of ' magical '), and -V displays all lines that do not contain matching text

With this mark

A more detailed explanation is as follows:

1The role of the grep command in a Linux system is a powerful text-search tool that can use regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users. 2. Formatgrep[Options]3main parameters [options] Main parameter:-C: Outputs only the count of matching rows. -I: Case insensitive (only for single-character). -H: The file name is not displayed when querying multiple files. -L: Only file names that contain matching characters are output when querying multiple files. -N: Displays matching lines and line numbers. -S: does not display error messages that do not exist or have no matching text. -V: Displays all lines that do not contain matching text. Pattern Regular expression main parameter: \: Ignores the original meaning of special characters in regular expressions. ^: Matches the start line of the regular expression. $: Matches the end line of the regular expression. \<: Starts with the line that matches the regular expression. \>: The end of the line to match the regular expression. []: A single character, such as [a], a meets the requirements. [ -]: range, e.g. [A-z], that is, a, B, C to Z all meet the requirements. : all the individual characters. *: There are characters, the length can be 0. 4The . grep command uses a simple instance of $grep' Test ' d*displays all rows that contain test in a file that begins with D. $ grep' Test ' AA bbccdisplays the line that matches test in the aa,bb,cc file. $ grep' [a-z]\{5\} ' AA displays all lines that contain a string of at least 5 consecutive lowercase characters for each string. $ grep‘W\ (es\) t.*\1′aa If West is matched, es is stored in memory, labeled 1, and then searched for any character (.*), these characters are followed by another ES (\1), locate the row that is displayed. If you use Egrep or GREP-E, you do not need the "\" number to escape, directly written as 'W(es) t.*\1you can have it. 5The . grep command uses a complex instance to assume that you are '/usr/src/linux/Doc ' Directory Search for files with string ' Magic ': $grepMagic/usr/src/linux/doc/*sysrq.txt:* How does I enable the Magic SysRq key?sysrq.txt:* How does I use the Magic SysRq key? Where the file ' Sysrp.txt ' contains the string that is discussed by Sy The SRQ function. By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form: Grep:sound:Is A directory This may make the output of ' grep ' difficult to read. There are two ways to resolve this: explicitly require search subdirectories: Grep-r or Ignore subdirectories: grep-d Skip if you have a lot of output, you can go through the pipeline to read it on ' less ': $ grep magic/usr/src/linux/ documentation/* | Less so, you can read more easily. One thing to note is that you need to provide a way to filter the file (search for all files in *). If you forget, ' grep ' will wait until the program is interrupted. If you are experiencing such a situation, press <ctrl c>, and then try again. Here are some interesting command-line arguments: grep-i pattern Files: Search is not case-sensitive. The default case is case-sensitive, grep-l. Files: Lists only the matching file names, grep-l the pattern files: Lists the mismatched file names, grep-w the pattern: matches only the entire word, not part of the string (such as Match ' Magic ', not ' magical '), grep-c number pattern files: matching contexts display [number] lines, grep pattern1 | PATTERN2 files: Displays rows matching Pattern1 or pattern2, grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2. Grep-n pattern files to display line number information grep-c the pattern files to find the total number of rows There are also some special symbols for searching:\< and \>, respectively, to mark the beginning and end of a word. For example: grep man * will match ' Batman ', ' manic ', ' man ' and so on, grep ' \<man ' match 'Manic ' and ' man ', but not ' Batman ', grep ' \<man\> ' only matches ' man ', not ' Batman ' or ' manic ' and other strings. ' ^ ': refers to a matching string at the beginning of the line, ' $ ': refers to a string of matching strings at the end of the line, Grep command usage Daquan 1, Parameters:-I: Ignore case-C: Print matching number of rows-L: Look for a match from multiple files-V: Find rows with matches-N: Print the row and row RE (regular expression) \ ignores the original meaning of special characters in regular expressions ^ matches the starting line of the regular expression to match the end line of the regular expression \< from the line that matches the regular expression to the end of the line that matches the regular expression \> [] a single character, such as [a] is a conforming to the requirements [-] range; A-z] that is a,b,c until Z meets the requirements. All single characters * All characters, length can be 03, example # Ps-ef | grep in.telnetdroot 19955 181 0 13:43:53? 0:00 in.telnetd# More Size.txt The contents of the size file B124230B034325A081016M7187998M7282064A022021A061048M9324822B103303A013386B044525M8987131B081016M45678B103303BADC 2345# more Size.txt | grep ' [A-b] ' range, such as [A-z] i.e. a,b,c all the time up to Z all meet the requirements b124230b034325a081016a022021a061048b103303a013386b044525# more Size.txt | grep ' [A-b] ' * b124230b034325a081016m7187998m7282064a022021a061048m9324822b103303a013386b044525m8987131b081016m45678b103303badc2345 # More Size.txt | grep ' B ' single character, e.g. [a] i.e. a meets requirements b124230b034325b103303b044525# more Size.txt | grep ' [BB] ' b124230b034325b103303b044525b081016b103303badc2345# 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 start line of the regular expression root::0:root# grep ' uucp '/etc/groupuucp::5:root,uucpnuucp::9:root,nuucp# grep ' \&LT;UUCP '/ETC/GROUPUUCP :: 5:root,uucp# grep ' root$ '/etc/group matches the end line of the regular expression root::0:rootmail::6:root# more Size.txt | Grep-i ' B1. "-I: Ignore case b124230b103303b103303# more Size.txt | Grep-iv ' B1. "-V" : Find rows that do not contain matches b034325a081016m7187998m7282064a022021a061048m9324822a013386b044525m8987131b081016m45678badc2345# More Size.txt | Grep-in ' B1. * * 1:b1242309:b10330315:b103303# grep ' $ '/etc/init.d/nfs.server | wc-l128# grep ' \$ '/etc/init.d/nfs.server | Wc–l ignores the original meaning of special characters in regular expressions 15# grep ' \$ '/etc/init.d/nfs.servercase "$" in>/tmp/sharetab.$$["x$fstype"! = Xnfs] &&am P;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 ' ^[]* (#|$) ' I f [$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: $ {Start | Stop} "# more Size.txtthe test filetheir is filesthe end# grep ' the ' size.txtthe test filetheir is files# grep ' \<the  ' Size.txtthe test filetheir is files# grep ' the\> ' size.txtthe test file# grep ' \<the\> ' size.txtthe test file# grep ' \<[tt]he\> ' size.txtthe test file==================================================================1, Introduction A multipurpose Text Search tool that uses regular expressions. This Php?name=%c3%fc%c1%ee "onclick=" Tagshow (event) "class=" T_tag "> Command was originally a php?name in the Ed line editor =%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 the file for all occurrences of the pattern, and the pattern can be either a string to search for or a regular expression. Note: When entering a string to search, it is best to use double quotation marks/when using regular expressions in pattern matching, note that single quotation mark 2 is used. The option for grep-C outputs only the count of matching rows-I is case-insensitive (for single-character)-n displays matching line numbers-V does not display no matching text so there is a line-s that does not display the error message-E uses extended regular expressions for more options see: Man grep3, commonly used grep instance (1) Multiple file query grep "sort" *.doc #见文件名的匹配 (2) Row match: Output matching row count grep-c "" Data.doc #输出文档中含有48字符的行数 (    3) Display matching rows and number of rows Grep-n "Data.doc #显示所有匹配48的行和行号 (4) shows unmatched rows grep-vn" "Data.doc #输出所有不包含48的行 (4) shows unmatched rows  GREP-VN Data.doc #输出所有不包含48的行 (5) Case sensitive grep-i "AB" Data.doc #输出所有含有ab或Ab的字符串的行4, application of regular expressions (1) application of regular expressions (Note: It is best to enclose the regular expression in single quotes) grep ' [239]. ' Data.doc #输出所有含有以2, 3 or 9, and two-digit line (2) mismatch test grep ' ^[^48] ' Data.doc #不匹    The line header is 48 lines (3) using the extended pattern matching grep-e ' 219|216 ' Data.doc (4) ... This needs to be applied and summed up in practice, mastering the regular expression skillfully. 5, use the class name to match the class name with the international pattern: [[: Upper:]] [a-z][[:lower:]] [a-z][[:d igit:]] [0-9][[:alnum:]] [0-9a-za-z][[:space:]] space or TA  B[[:alpha:] [a-za-z] (1) Use grep ' 5[[:upper:]][[:upper:]] ' Data.doc   #查询以5开头以两个大写字母结尾的行 

by a Linux grep command.

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.