grep Learning Notes
finishing: Jims of fat fat family
<yjnet@21cn.com>
copyright©2004 This article complies with the GPL agreement, welcome to reprint, revise, spread.
First release date: July 16, 2004
Table of Contents 1. grep profile 2. grep regular Expression meta-character set (base set) 3. Meta-character extension set 4 for Egrep and GREP-E. POSIX character Class 5. grep command option 6. Example 1-grep introduction
grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is an extension of grep that supports more re metacharacters, Fgrep is fixed grep or fast grep, which regards all letters as words, that is, the metacharacters in the regular expression returns to its own literal meaning and is no longer special. Linux uses the GNU version of grep. It is more powerful and can use the EGREP and FGREP functions through the-G,-E,-F command-line Options.
grep works like this by searching for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to the screen without affecting the contents of the original file.
grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 2 if the searched file does not exist. We can use these return values to do some automated text processing work. 2. grep regular expression meta-character set (base set) ^
The beginning of the anchoring line, such as: ' ^grep ' matches all rows beginning with grep. $
End of anchor line such as: ' grep$ ' matches all rows with grep ending ...
Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P. *
Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces followed by the grep row ... * together to represent any character. []
Matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep. [^]
Matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with A-r and T-z, followed by the rep line. /(.. /)
Tags match characters, such as '/(love/) ', and Love is marked as 1. /<
Anchors the beginning of a word, such as: '/<grep ' matches a line containing a word that starts with grep. />
Anchors the end of a word, such as ' grep/> ' to match a line containing a word that ends with grep. x/{m/}
Repeat characters x,m times, such as: ' 0/{5/} ' matches rows containing 5 O. x/{m,/}
Repeat character X, at least m times, such as: ' o/{5,/} ' matches rows with at least 5 O. x/{m,n/}
Repeat character X, at least m times, no more than n times, such as: ' o/{5,10/} ' matches 5--10 O's line. /w
Matches literal and numeric characters, that is, [a-za-z0-9], such as: ' G/w*p ' matches with G followed by 0 or more text or number characters, followed by P. /w
/w, which matches one or more non word characters, such as the dot number period. /b
Word locks, such as: '/bgrep/b ' only match grep. 3. Meta-character extension set for Egrep and grep-e +
Matches one or more of the previous characters. such as: ' [a-z]+able ', match one or more lowercase letters followed by the able string, such as loveable,enable,disable.?
Matches 0 or more previous characters. For example: ' Gr?p ' matches a gr followed by one or no characters, then a line of P. A|b|c
Match A or B or C. such as: grep|sed match grep or sed ()
Group symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov. X{m},x{m,},x{m,n}
function with x/{m/},x/{m,/},x/{m,n/} 4. POSIX character Classes
POSIX (The Portable operating System Interface) adds special character classes, such as [: Alnum:], to keep one to the character encodings in different countries. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux, grep supports the POSIX character classes except Fgrep. [: Alnum:]
literal numeric characters [: Alpha:]
Text characters [:d Igit:]
numeric characters [: graph:]
Non-null characters (not spaces, control characters) [: Lower:]
lowercase characters [: cntrl:]
control characters [:p rint:]
Non-null characters (including spaces) [:p UNCT:]
Punctuation mark [: Space:]
All white-space characters (new lines, spaces, tabs) [: Upper:]
Uppercase characters [: Xdigit:]
hexadecimal digit (0-9,A-F,A-F) 5. grep command Options -?
Displays both the top and bottom of the matching row. Rows, such as: grep-2 pattern filename Displays the top and bottom 2 rows of matching rows at the same time. -b,--byte-offset
Print the block number in which the line is printed before the matching line. -C,--Count
Prints only the number of rows that match and does not display the matching content. -F File,--file=file
Extracts the template from the file. The empty file contains 0 templates, so nothing matches. -h,--no-filename
When searching for multiple files, the matching filename prefix is not displayed. -i,--ignore-case
ignores case differences. -q,--quiet
Suppresses display and returns only the exit status. 0 indicates that a matching row was found. -l,--files-with-matches
Print a list of files that match the template. -l,--files-without-match
Print a list of files that do not match the template. -n,--line-number
Print the line number before the matching line. -s,--silent
Does not display error messages about the absence or inability to read files. -v,--revert-match
Reverse retrieve, showing only rows that do not match. -w,--word-regexp
If referenced by/< and/>, the expression is searched as a word. -v,--version
Displays software version information. 6. Examples
To use a good grep this tool, in fact, is to write a good regular expression, so here is not the grep all the features of the example to explain, only a few examples, explain a regular expression of the wording. $ ls-l | grep ' ^a '
Filters the contents of the LS-L output through a pipe, showing only the rows that start with a. $ grep ' test ' d*
Displays the rows that contain test in all files that start with D. $ grep ' test ' AA bb cc
Displays the row that matches test in the aa,bb,cc file. $ grep ' [a-z]/{5/} ' AA
Displays all the lines that contain at least 5 consecutive lowercase characters for each string. $ Grep ' w/(es/) T.*/1 ' AA
If West is matched, the es are stored in memory and labeled 1, then search for any character (. *), followed by another ES (/1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "/" number to escape, directly written ' W (es) T.*/1 ' on it.
Some examples Cathy ~> grep root/etc/passwd Root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin Cathy ~> grep-n root/etc/passwd 1:root:x:0:0:root:/root:/bin/bash 12:operator:x:11:0:operator:/root:/sbin/ Nologin Cathy ~> Grep-v bash/etc/passwd | Grep-v nologin Sync:x:5:0:sync:/sbin:/bin/sync Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin :/sbin/halt news:x:9:13:news:/var/spool/news:mailnull:x:47:47::/var/spool/mqueue:/dev/null Xfs:x:43:43:X Font Server:/etc/x11/fs:/bin/false rpc:x:32:32:portmapper RPC User:/:/bin/false NSCD:X:28:28:NSCD Daemon:/:/bin/false Named:x:25:25:named:/var/named:/bin/false squid:x:23:23::/var/spool/squid:/dev/null Ldap:x:55:55:LDAP User:/var/ Lib/ldap:/bin/false apache:x:48:48:apache:/var/www:/bin/false Cathy ~> grep-c false/etc/passwd 7 Cathy ~> Grep-i PS ~/.bash* | Grep-v history/home/cathy/.bashrc:ps1= "/[/033[1;44m/] $USER is in/w/[/033[0m/]" from the previous example, we want to show only those lines that start with the string "root": Cat Hy ~> grep ^rooT/ETC/PASSWD Root:x:0:0:root:/root:/bin/bash If we want to see which account and what shell is not assigned, we search the line terminator ":": Cathy ~> grep: $/etc/passwd News: X:9:13:news:/var/spool/news: To check whether path is exported in ~/.BASHRC, first select "Export" and then search for the line starting with the string path so that no manpath or other possible paths are searched: Cathy ~> grep Export ~/.BASHRC | grep '/Match end of Word. If you want to find a string of delimited words (surrounded by spaces), it's best to use-W, just as in this example we are in the actual root partition information: Cathy ~> grep-w//etc/fstab label=//ext3 defaults 1 1 If you do not use this option, all rows in the system table will be displayed. Cathy ~> grep [YF]/etc/group sys:x:3:root,bin,adm tty:x:5:mail:x:12:mail,postfix ftp:x:50:nobody:x:99:floppy:x:19 : xfs:x:43:nfsnobody:x:65534:postfix:x:89:cathy ~> ls *[1-9].xml app1.xml chap1.xml chap2.xml chap3.xml chap4.xml CA Thy ~> grep '/'/usr/share/dict/words catch clash cloth coach Couch cough crash crush Cathy ' ~> grep '/'/usr/share/d Ict/words Caliph Cash Catch cheesecloth Cheetah Cathy ~> grep */etc/profile Cathy ~> grep-f ' * '/etc/profile for I in/etc/profile.d/*.sh; Todo