first, let's look at the grep command, grep (global search regular expression and printing), a full search for regular expressions and print the lines, is a powerful text search tool that can use regular expressions to search for text, and print out the matching lines. The grep commands in Linux include, grep, Egrep, and Fgrep. Egrep is an extension of grep that supports more re-metacharacters, and Fgrep is fast grep, which searches for characters faster but does not support regular expressions.
then we'll familiarize ourselves with the grep command, which has the following command format: grep [options] ' pattern ' FILE. Among the common options for grep:
-V: Reverse selection, showing only rows that do not conform to the pattern;
- o: Displays only strings that are matched by the pattern, not the entire row;
-I: does not distinguish between uppercase and lowercase characters;
- R: Recursive search;
-A #: Displays the line that matches to the row, passing in the following lines;
- B #: Displays the line that matches to the row, passing in the preceding # lines;
- C #: Displays the line that matches to the row, passing in and out of it;
In addition, for the user to view the results of the display match, you can enter "--color=auto" after the grep command to render the result color.
--color to display filtered content in color
[[email protected] ~]# grep--color ' root '/etc/passwd root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root: /sbin/nologin
Shows in which line-n
[[email protected] ~]# grep--color-n ' root '/etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator :/root:/sbin/nologin
To make it easier for us to alias.
[email protected] ~]# alias cg= ' grep--color ' [[email protected] ~]# vim. bashrc#. bashrc# User specific aliases and funct Ionsalias rm= ' rm-i ' Alias cp= ' cp-i ' Alias mv= ' mv-i ' Alias cg= ' grep--color ' # Source global definitionsif [-F/ETC/BASHRC ]; Then. /etc/bashrcfi
Test it.
[[email protected] ~]# cg-n ' root ' 1.txt1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/ Nologin
Just want to show the number of rows
[[email protected] ~]# cg-c ' root ' 1.txt2
Take the anti-root not included
[[email protected] ~]# cg -v ' Root ' 1.txtbin:x:1:1:bin:/bin:/sbin/nologindaemon: x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/ nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/ Haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologingames:x : 12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:ftp user :/var/ftp:/sbin/nologinnobody:x:99:99:nobody:/:/sbin/nologindbus:x:81:81:system message bus:/:/sbin/ nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinrpc:x:32:32:rpcbind daemon:/var/cache/rpcbind:/sbin/nologinsaslauth:x:499:76:saslauthd user:/var/empty/saslauth:/sbin/ nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinhaldaemon:x:68:68:hal daemon:/:/sbin/ Nologinrpcuser:x:29:29:rpc service user:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:anonymous nfs user:/var/lib/nfs:/sbin/ nologinsshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/nologinmysql:x:500:500::/home/mysql:/ Sbin/nologin
[[email protected] ~]# cg -n -a 2 ' Root ' 1.txt display matches to the row, passing in the following # lines; 1:root:x:0:0:root:/root:/bin/bash2-bin:x:1:1:bin:/bin:/sbin/nologin3-daemon:x : 2:2:daemon:/sbin:/sbin/nologin--11:operator:x:11:0:operator:/root:/sbin/nologin12-games:x:12:100:games:/usr/ Games:/sbin/nologin13-gopher:x:13:30:gopher:/var/gopher:/sbin/nologin[[email protected] ~]# cg -n -B 2 ' Games ' 1.txt displays the line that matches to the row, showing the # lines in front of it; 10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin11-operator:x : 11:0:operator:/root:/sbin/nologin12:games:x:12:100:games:/usr/games:/sbin/nologin[[email protected] ~]# cg -n -C 2 ' Games ' 1.txt displays the line that matches to the row, and then shows the # lines before and after it; 10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin /nologin11-operator:x:11:0:operator:/root:/sbin/nologin12:games:x:12:100:games:/usr/games:/sbin/nologin13-gopher:x:13:30:gopher:/var/ Gopher:/sbin/nologin14-ftp:x:14:50:ftp user:/var/ftp:/sbin/nologin
This article is from "Linux rookie" blog, please be sure to keep this source http://490617581.blog.51cto.com/11186315/1759980
grep command and regular expression (filter) one