Linux Learning (23) Regular expression (i) Grep/egrep

Source: Internet
Author: User
Tags egrep

I. Overview

Regular expressions are the knowledge that is often needed in operational operations. File lookups, log parsing, rewrite rules, shell scripts, and so on, all require the knowledge of regular expressions. So what is a regular expression? My understanding is to find or replace the string you need according to certain rules, which is a regular expression, exactly, it is a rule. There are many commands to use regular expressions, and the grep and egrep we're talking about today are the most common ones. There's also sed, awk, and we'll talk about that later.

Second, Grep/egrep

Egrep is an enhanced version of grep, and Egrep can be implemented by grep, and in some places it is more convenient to use Egrep. Here are some details about these two commands.

Grammar:grep  [-cinvABC]  ‘word‘  filename

-C: Print the number of lines that meet the requirements

-I: Ignore case

-N: Output with the same number of lines as required

-V: Print rows that do not meet the requirements

-A: followed by a number (with or without spaces), for example, –A2 to print the line that meets the requirements and the following two lines

-B: followed by a number, such as –B2, to print the line that meets the requirements and the above two lines

-C: followed by a number, such as –C2, to print the line that meets the requirements and two rows above and below

Experiment:

-C

' oo ' a.txt 6

-I.

[[email protected] test]$ grep--color-i'OO'a.txtroot:x:0:0: root:/root:/bin/bashlp:x:4:7: lp:/var/spool/lpd:/sbin/nologinmail:x:8: A: mail:/var/spool/mail:/sbin/nologinuucp:x:Ten: -: uucp:/var/spool/uucp:/sbin/Nologinoperator: x: One:0:operator:/root:/sbin/nologinpostfix:x: the: the::/var/spool/postfix:/sbin/nologin

The--color option means highlighting what you are searching for:

-N

[Email protected] test]$ grep-n'oo'a.txt1: root:x:0:0: root:/root:/bin/Bash5: lp:x:4:7: lp:/var/spool/lpd:/sbin/Nologin9: mail:x:8: A: mail:/var/spool/mail:/sbin/NologinTen: uucp:x:Ten: -: uucp:/var/spool/uucp:/sbin/Nologin One:operator: x: One:0:operator:/root:/sbin/Nologin +:p ostfix:x: the: the::/var/spool/postfix:/sbin/nologin

-V

' oo ' 3 bin:x: 1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/  NOLOGINADM:X:3:4: adm:/var/adm:/sbin/nologin

-A

' Li\.jianlin ' a.txt li.jianlin:x:7202:7022::/home/li.jianlin:/bin/bash

Because 93 rows are the last line, only the folded lines are displayed

-B

' Li\.jianlin ' a.txt -wang.xishuai:x:7200:7022::/home/wang.xishuai:/bin/bash- Han.xinyu:x:7201:7022::/home/han.xinyu:/bin/bash: li.jianlin:x: 7202:7022::/home/li.jianlin:/bin/bash

-C

' Xishuai ' a.txt -xiong.zhengmao:x:7199:7022::/home/xiong.zhengmao:/bin/bash: WANG.XISHUAI:X:7200:7022::/home/wang.xishuai:/bin/bash-han.xinyu:x :7201:7022::/home/han.xinyu:/bin/bash

Special character "."

Special characters. Denotes any character

' 7.22 ' a.txtmabg:x: 7032:7022::/home/mabg:/bin/bashzhongwt:x:7038:7022::/HOME/ZHONGWT :/bin/bash

. * denotes any number of arbitrary characters (can contain blank lines)

' .* ' A.TXT|WC-l

Visible use. * Find all rows.

It means no, or there are 1 of them.

[[email protected] test]$ grep"n?"A.txt[[email protected]-zol-fss-web1 test]$ grep"n\?"A.txt|head-n3root:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/Nologin[[email protected]-zol-fss-web1 test]$ Egrep'n?'A.TXT|HEADN-N3-Bash:headn:command not found[[email protected]-zol-fss-web1 test]$ Egrep'n?'A.txt|head-n3root:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d Aemon:/sbin:/sbin/nologin

From the above example we can find that using grep can not directly determine the special symbol? , but it needs to be escaped, but not escaped with Egrep.

In addition to, EGREP supports + (indicates one or more):

[[email protected] test]$ grep'o+'A.txt|head-N3[[email protected]-zol-fss-web1 test]$ Egrep'o+'A.txt|head-n3root:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/Nologin[[email protected]-zol-fss-web1 test]$ grep'o\+'A.txt|head-n3root:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d Aemon:/sbin:/sbin/nologin

Use () in a regular expression to represent a whole:

' (Oo|ol) ' A.txt[[email protected]'(oo|ol)' a.txt|head-n3root:x:0: 0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:  2:2:d aemon:/sbin:/sbin/nologin

grep can also search for related rows in the specified file or directory:

[Email protected] test]$ grep-r--include='*.txt' 'oo\+'././A.TXT:ROOT:X:0:0: root:/root:/bin/bash./A.TXT:LP:X:4:7: lp:/var/spool/lpd:/sbin/Nologin./A.TXT:MAIL:X:8: A: mail:/var/spool/mail:/sbin/Nologin./A.TXT:UUCP:X:Ten: -: uucp:/var/spool/uucp:/sbin/Nologin./a.txt:operator: x: One:0:operator:/root:/sbin/Nologin./A.TXT:POSTFIX:X: the: the::/var/spool/postfix:/sbin/nologin

-R is the meaning of recursive search,--include= "*.txt" means searching for a file ending in. txt. Note that when using grep, you need to escape if you use the + sign in the regular.

Linux Learning (23) Regular expression (i) Grep/egrep

Related Article

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.