Personal Insights: I think the text processing commands in Linux the grep egrep fgrep command is just as easy as mastering grep because the other two are almost the same, and the difference is very good.
grep Basic Use method:
Using the method grep [option] ' expression ' #学习的时候没好好听 don't know that I wrote it wrong, not for this post. Usually I don't read the video I studied before.
The common options here are-o-v-i-n-a-b-C-Digital-E
[email protected]:~# grep -o ' ^root: ' /etc/passwd #-o option is to display only matches to the content root: [email protected]:~# grep -v ' ^root: ' /etc/passwd #-v The option is to display a line of content that is not a match daemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/shsync:x : 4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/bin/shman:x:6:12:man:/var/cache/man:/bin/shlp:x:7:7 : Lp:/var/spool/lpd:/bin/shmail:x:8:8:mail:/var/mail:/bin/shnews:x:9:9:news:/var/spool/news:/bin/shuucp:x:10:10 : Uucp:/var/spool/uucp:/bin/shproxy:x:13:13:proxy:/bin:/bin/shwww-data:x:33:33:www-data:/var/www:/bin/shbackup: X:34:34:BACKUP:/VAR/BACKUPS:/BIN/SHLIST:X:38:38:MAILING LIST MANAGER:/VAR/LIST:/BIN/SHIRC:X:39:39: ircd:/var/run/ircd:/bin/shgnats:x:41:41:gnats bug-reporting system (Admin):/var/lib/gnats:/bin/ SHNOBODY:X:65534:65534:NOBODY:/NONEXISTENT:/BIN/SHLIBUUID:X:100:101::/VAR/LIB/LIBUUID:/BIN/SHSYSLOG:X:101:103: :/home/syslog:/bin/falsemessagebus:x:102:105::/var/run/dbus:/bin/falsebind:x:103:108::/var/cache/bind:/bin/falselandscape:x:104:109::/var/lib/ landscape:/bin/falsesshd:x:105:65534::/var/run/sshd:/usr/sbin/nologinmariadb:x:1000:1000::/home/mariadb:/sbin/ nologinwww:x:1001:1001::/home/www:/sbin/nologin [email protected]:~# grep -n ' ^root: ' The /etc/passwd #-n option is to display the line 1:root:x:0:0:root:/root:/bin/bash [email protected in the file where the matching row is located]: ~# grep -a1 ' ^root: ' /etc/passwd #-a digital is displaying matching rows and displaying n rows down root:x:0:0:root:/root:/bin/ bashdaemon:x:1:1:daemon:/usr/sbin:/bin/sh [email protected]:~# grep -b1 ' ^daemon: ' /etc/passwd #-b digital is to display matching rows and display n rows up root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/ bin/sh [email protected]:~# grep -c1 ' ^daemon: ' /etc/passwd #-c digital is to display the matching rows and display the n rows up and down root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/sh [email protected]:~# grep -1 ' ^daemon: ' /etc/passwd #-number option with-croot:x:0:0:root:/root:/bin/ Bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/sh [email protected]:~# grep -i ' ^root: ' /etc/passwd #-i option actually ignores the case of matching content Root:x:0:0:root:/root:/bin/bash
Here, the-e option is taken separately, because this thing is sometimes overlooked. Using the-E option grep can use regular extension expressions, or grep can use only standard regular expressions
Specific use or wait below to say it.
grep character matching:
We also have a number of character set matching features in regular expressions #个人见解 These character sets need to be well remembered for the simplicity of code and easier use of regular expressions in future use
* #表示 * Pre-character any number of times including 0 \? #表示 \ Previous content appears 0 or 1 times \{m\} #表示它前的字符要出现m次 \{m,n\} #表示它前的字符至少出现m次, at least n times \{m,\} #表示它前的字符至少出现m次 \{0,n\} #表示它前的字符最多出现n次. #匹配单个任意的字符 [] #匹配指定范围内的任意单个字符. * #这里就是表示任意长度的任意字符 [[:d igit:]] #匹配0-9 of a single number, here is not think [0-9] writing better? In fact, when using regular expressions, you will find [[:d igit:]] easier to use [[: Lower:]] #匹配单个小写字母同 [a-z][[:upper:]] #匹配单个大写字母同 [a-z][[:space:]] #匹配单个空白字符 including spaces tab [[: Alpha:]] #匹配单个大小写字母 [[:p UNCT:]] #匹配单个标点符号 [^[:p UNCT:]] This is actually the following [[: Alnum]][[:alnum:]] #匹配单个非标点的字符 [^[:alnum:]] This is the above [[:p unct:]][^] #通过上面两行就不难理解了 [^] is the reverse meaning:
the anchoring of grep:
^ #行首锚定 $ #行尾锚定 ^$ #空白行 \< #词首锚定也可以用 \b\> #词尾锚定也可以用 \b
grep grouping
The grouping needs to use \ (\) For example: \ (ab\) * The contents of the pattern matching in the grouping, which can be remembered in memory by the regular expression engine, and can then be referenced by a reference: for example \ (ab\ (x\) y\) c\) \) mn\ (x\) \). *\1#: Refers to the content that the nth parenthesis matches to, not the schema itself, for example: \ (ab\?c\). *\1
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3C/B9/wKioL1PDIInBVUjNAAFx_8R8lkA107.jpg "title=" linux-grep-grouping-using method 11.png "alt=" Wkiol1pdiinbvujnaafx_8r8lka107.jpg "/>
as we refer to the use of the method is: Starting from the left is the number opening parenthesis Encounter one is a group
Example:
1. Display lines beginning with uppercase or lowercase s in the/proc/meminfo file;# grep -i ' ^s ' /proc/meminfo# grep ' ^[ss] ' /proc/meminfo# grep -e ' ^ (s|s) ' /proc/meminfo2, display/etc/passwd file whose default shell is non-/sbin/nologin user; # grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f13, display/etc/ The user whose default shell is/bin/bash in the passwd file; further: Display only the user;# grep "/bin/bash$" whose ID number is the highest in the above results /etc/passwd | sort -t: -k3 -n | tail -1 | cut -d: -f14, find/etc/ One or two digits in the passwd file;# grep "\<[0-9][0-9]\?\>" /etc/passwd# grep "\<[0-9]\{1,2\ }\> " /etc/passwd5, displays/boot/grub/grub.conf with at least one whitespace character;# grep " ^[[:space:]]\{1,\} " / Boot/grub/grub.conf6, display the/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one line of non-whitespace characters;# grep "^#[[:space:] ]\{1,\}[^[:space:]]\{1,\} " /etc/rc.d/rc.sysinit7, find the line that ends with ' LISTEN ' in the Netstat -tan command execution result; # netstat -tan | grep "listen[[:space:]]*$ "8, add user Bash, testbash, basher, nologin (shell is/sbin/nologin), and find out that the user name on the current system is the same as the default shell user;# grep "^\ ([[: Alnum:]]\{1,\}\):. *\1$" /etc/passwd9, extension question: Create a new text file, Assume the following: He like his lover. He love his lover. He like his liker. He love his liker. Find out that the last word is a line consisting of a previous word plus r. \ (L.. e\). *\1r
the difference between egrep Fgrep and grep
The difference between Egrep and grep