Use a regular expression

Creative Commons License Creative Commons

Question
in this case require familiarity with regular expressions to write, perform the following tasks:
using the tool egrep regular exercise basic usage of expressions
to extract valid configuration file httpd.conf line of
writing regular expressions that match the MAC address, E-Mail E-mail address , IP address, host name of the
program
steps
to achieve this case need to follow the steps below.

Step one: regular expression matching exercises
* 1) Typical applications: grep, egrep retrieve lines of text
when using the grep command with the -E option, support for basic regular match mode. Such as "word" keyword search, "^ word" match in a row at the beginning of the word, "word $" matches the end of the word line ...... and so on.
Output "r" at the beginning of user records:
*

[root@svr5 ~]# grep '^r' /etc/passwd
root:x:0:0:root:/root:/bin/bash
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
输出以“localhost”结尾的行:
[root@svr5 ~]# grep 'localhost$' /etc/hosts
127.0.0.1               localhost.localdomain localhost

If you wish to combine multiple conditions simultaneously grep search query, such as the output as "root" or line with "daemon" at the beginning, this time on a regular base less convenient ( "or" must be escaped as "|"):

[root@svr5 ~]# grep '^root|^daemon' /etc/passwd  		//搜索无结果
[root@svr5 ~]#
[root@svr5 ~]# grep '^root\|^daemon' /etc/passwd  		//正确获得结果
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Use grep -E and Ruoruo or egrep command supports extended regular pattern matching can automatically identify |, regular expressions like {special characters, more convenient to use, such as:

[root@svr5 ~]# grep -E '^root|^daemon' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
或者
[root@svr5 ~]# egrep '^root|^daemon' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Use grep -E and egrep command using the exact equivalent, recommended the latter, especially when it comes to complex regular expressions.
2) -q option grep, egrep command
option -q quiet expressed (silent) means, in conjunction with this option is not only retrieval output, typically used to find the target recognition exists in the script, by returning a status $ ? to judge, so text can ignore irrelevant information, simplified script output.
For example, if there is an examination / etc / hosts file mapping records 192.168.4.4, if there is "YES" is displayed, otherwise output "NO", the general will execute:

[root@svr5 ~]# grep '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
192.168.4.4     svr5.tarena.com svr5
YES

Such mixed output prompt information and the script after the determination grep together, the user is not easy to identify, it can be changed to the following:

[root@svr5 ~]# grep -q '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
YES

Is not much cleaner, can also be seen from the above results, the effect is similar to the -q option of using &> / dev / null results.

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91947211