Linux grep usage
grep command
Function: Finds a string in each line of the input file.
Basic usage:
grep [-ACINV] [--color=auto] [-A n] [-B N] ' search string ' file name
Parameter description:
-A: Binary documents are processed in text mode
-C: Show number of matches
-I: Ignore case differences
-N: Show line numbers at the beginning
-a:after, displays data that matches n rows after the string
-b:before, displays data that matches the first n rows of a string
-V: Shows the meaning of no matching line-a:after, shows the meaning of the N-line-b:before after the matched part, displays the first n rows of the matching part
--color: Highlight matching keywords in a specific color
The –color option is a great option for you to clearly understand which characters are matched. It is best to include in your own. BASHRC or. bash_profile file:
Alias Grep=grep--color=auto
After each grep search, the matching effect is automatically highlighted.
The ' search string ' is a regular expression, note that in order to avoid the effect of the shell's metacharacters on regular expressions, enclose it in single quotation marks (""), and never enclose it in double quotation marks ("").
Regular expressions are divided into basic regular expressions and extended regular expressions. Here is a brief summary of each.
Basic Regular Expressions
Regular expression learning is mainly about regular expression meta-data learning. There is nothing advanced in the regular expression itself, this article simply summarizes the meta-data of the basic regular expression:
Meta data
Meaning and examples
(1) ^root Search for lines starting with root.
For example: Search for a script that starts with root
[Email protected] shell]# grep-n ' ^root '/etc/passwd1:root:x:0:0:root:/root:/bin/bash
(2) word$ Search for lines ending in Word
For example, search for lines that end with ' Nologin '
[[email protected] shell]# grep -n ' nologin$ ' /etc/passwd2:bin:x:1:1:bin:/bin:/ sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/ spool/lpd:/sbin/nologin9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10: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/nologin15:nobody:x : 99:99:nobody:/:/sbin/nologin16:dbus:x:81:81:system message bus:/:/sbin/nologin17:usbmuxd:x:113:113: Usbmuxd user:/:/sbin/nologin18:rpc:x:32:32:rpcbind daemon:/var/cache/rpcbind:/sbin/nologin19:o Profile:x:16:16:special user account to be used by oprofile:/home/oprofile :/sbin/nologin20:vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin21:rtkit:x:499:497 : realtimekit:/proc:/SBIN/NOLOGIN22:ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN23:HSQLDB:X:96:96::/VAR/LIB/HSQLDB:/SBIN/NOLOGIN24: AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN25:APACHE:X:48:48: apache:/var/www:/sbin/nologin26:saslauth:x:498:76: "Saslauthd user":/var/empty/saslauth:/sbin/nologin27: postfix:x:89:89::/var/spool/postfix:/sbin/nologin28:rpcuser:x:29:29:rpc service user:/var/lib/nfs:/ Sbin/nologin29:nfsnobody:x:65534:65534:anonymous nfs user:/var/lib/nfs:/sbin/nologin30:haldaemon:x : 68:68:hal daemon:/:/sbin/nologin31:gdm:x:42:42::/var/lib/gdm:/sbin/nologin32:ntp:x:38:38::/etc/ntp:/sbin /nologin33:pulse:x:497:495:pulseaudio system daemon:/var/run/pulse:/sbin/nologin34:webalizer:x:67:67 : webalizer:/var/www/usage:/sbin/nologin35:sshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/ nologin36:tcpdump:x:72:72::/:/sbin/nologin40:cloudera-scm:x:496:492:cloudera manager:/var/lib/ Cloudera-scm-server:/sbin/nologin43:solr:x:493:488:solr:/var/lib/solr:/sbin/nologin52:sqoop2:x:484:479:sqoop 2 user:/var/lib/sqoop2:/sbin/ nologin55:sentry:x:481:476:sentry:/var/lib/sentry:/sbin/nologin57:spark:x:479:474:spark:/var/lib/spark:/sbin/ Nologin61:zabbix:x:477:472:zabbix monitoring system:/var/lib/zabbix:/sbin/nologin
(3). Match any one character.
Example: Grep–n ' E.E ' regular.txt
Matches between E and E have any one character, can match eee,eae,eve, but does not match the EE.
(4) \ Escape character
For example: Search ', ' is a special character that has a special meaning in the regular expression. Must be escaped first.
Grep–n ' \ ' Regular.txt
(5) * The preceding character repeats 0 to several times.
such as matching gle,gogle,google,gooogle, etc.
Grep–n ' Go*gle ' regular.txt (o* means o can occur 0 or more times)
[[email protected] shell]# cat aaaaa.txt gle,gogle,google,gooogle[[email protected] shell]# grep-n ' Go*gle ' Aaaaa.txt 2: Gogle,3:google,4:gooogle[[email protected] shell]#
(6) [List] matches one of a series of characters.
[[email protected] shell]# cat aaaaa.txt gle,gogle,google,gooogle[[email protected] shell]# grep-n ' [o] ' aaaaa.txt 2:GOGL E,3:google,4:gooogle
(7) [N1-N2] matches one character in a range of characters.
Example: Match numeric characters [0-9]
[[email protected] shell]# cat aaaaa.txt gle,gogle,google,gooogle12231223hh[[email protected] shell]# grep-n ' [0-9] ' AAA Aa.txt 5:12236:1223hh
(8) [^list] matches characters other than the character set
[[email protected] shell]# cat aaaaa.txt gle,gogle,google,gooogle12231223hh[[email protected] shell]# grep-n ' [^e] ' AAAA A.txt 1:gle,2:gogle,3:google,4:gooogle5:12236:1223hh
(9) Match non-O character
\{n1,n2\} characters in front of repeat n1,n2 times
For example: Match Google,gooogle.
[email protected] shell]# cat aaaaa.txt gle,gogle,google,gooogle12231223hhgoogleasssgoogleasss[[email protected] shell]# grep-n ' go\{2,3\}gle ' aaaaa.txt 3:google,4:gooogle7:googleasss8:googleasss[[email protected] shell]#
(ten) The \<word Word is the beginning.
Example: Match a word that starts with G
[Email protected] shell]# grep-n ' \<g ' aaaaa.txt 1:gle,2:gogle,3:google,4:gooogle7:googleasss8:googleasss
(one) word\> matches the end of the word
Example: Matches a word ending with s
[Email protected] shell]# grep-n ' s\> ' aaaaa.txt 7:googleasss8:googleasss
Extending regular Expressions
grep generally supports basic regular expressions, which can be extended with parameter-e support.
In addition, GREP provides a separate extension command called Egrep to support extended regular expressions, which is equivalent to GREP-E.
In general, though, the basic regular expression is sufficient. In special cases, complex extended expressions can simplify the matching of strings.
The extended regular expression is based on the basic regular expression, and adds some meta-data.
Meta data
Meaning and examples
(12) + Repeat the preceding character 1 to multiple times
For example: Match God,good,goood and so on string.
[email protected] shell]# cat Aaaaa.txt Gle,gogle,google,gooogle12231223hhgoogleasssgoogleasssgoogoodgoood[[email Protected] shell]# grep-ne ' go+d ' aaaaa.txt 10:good11:goood
(13)? Match 0 or 1 preceding characters
[email protected] shell]# cat Aaaaa.txt gle,gogle,google,gooogle12231223hhgoogleasssgoogleasssgoogoodgooodgod[[ Email protected] shell]# grep-ne ' go?d ' aaaaa.txt 12:god
(14) | or (or) to match multiple strings
Example: Grep–ne ' God|good ' regular.txt
Match God or good
[email protected] shell]# cat Aaaaa.txt gle,gogle,google,gooogle12231223hhgoogleasssgoogleasssgoogoodgooodgod[[ Email protected] shell]# grep-ne ' God|good ' aaaaa.txt 10:good12:god
(15) () matches the entire string in parentheses, which is the same as a single character
For example: Search for good or glad
[email protected] shell]# cat Aaaaa.txt Gle,gogle,google, Gooogle12231223hhgoogleasssgoogleasssgoogoodgooodgodhhladgladdddd[[email protected] shell]# grep-ne ' g (la|oo) ' Aaaaa.txt 3:google,4:gooogle7:googleasss8:googleasss9:goo10:good11:goood15:gladdddd
The regular expressions under Linux are profound, the above support summarizes the most commonly used parts, if proficient in the above part of the regular expression can basically meet the daily use.
In addition, many Linux commands support regular expressions, such as Find,sed,awk and so on. Please use regular expressions when using the manual reference to these commands.
This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1826116
Linux grep usage