sed -i '$a IPADDR=192.168.1.199' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0            #追加ip地址。
sed -i '$aNETMASK=255.255.255.0' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0 #追加子网掩码
sed -i '$aGATEWAY=192.168.1.1' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0 #追加网关
sed -i '$aDNS1=114.114.114.114' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0 #追加DNS.
sed -i '/BOOTPROTO=/s/dhcp/static/g' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0 #修改ip获取方式。
sed -i '/ONBOOT=/s/no/yes/g' /etc/sysconfig/ifcfg-eth0work-scripts/ifcfg-eth0 #开机自启网卡。
[root@node2 ~]# grep "root" /etc/passwd
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
[root@node2 ~]# grep -i "Root" /etc/passwd
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
[root@node2 ~]# grep -in "Root" /etc/passwd
:root:x:::root:/root:/bin/bash
:operator:x:::operator:/root:/sbin/nologin
[root@node2 ~]# grep -ino "Root" /etc/passwd
:root
:root
:root
:root
[root@node2 ~]# grep -vc "root" /etc/passwd [root@node2 ~]# cat /etc/passwd |wc -l
[root@node2 ~]# grep -A  "core id" /proc/cpuinfo
core id :
cpu cores :
apicid :
[root@node2 scprits]# cat > demo.c << eof
> #inclued <stdio.sh>
> int main ()
> {
> int hello=
> int world=;
> int helloworld=hello+world
> printf ("&d\n", helloworld);
> return;
> }
> eof
[root@node2 scprits]# grep "hello" demo.c
int hello=
int helloworld=hello+world
printf ("&d\n", helloworld);
[root@node2 scprits]# grep "hello[[:upper:]]" demo.c
int helloWorld=hello+world
printf ("&d\n", helloWorld);
[root@node2 scprits]# grep "hello[^[:upper:]][[:digit:]]" demo.c
int hello=
[root@node2 scprits]# grep "hell[a-z]" demo.c
int hello=
int helloWorld=hello+world
printf ("&d\n", helloWorld);
[root@node2 scprits]# grep "hell[a-z][[:punct:]]" demo.c
int hello=
int helloWorld=hello+world

匹配次数:

      \{m,n\} :匹配其前面出现的字符至少m次,至多n次。
      \? :匹配其前面出现的内容0次或1次,等价于\{0,1\}。
      * :匹配其前面出现的内容任意次,等价于\{0,\},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。

[root@node2 scprits]# grep "/.*sh" /etc/passwd
root:x:::root:/root:/bin/bash
shutdown:x:::shutdown:/sbin:/sbin/shutdown
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
liujunjun:x::::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "/.\{0,2\}sh" /etc/passwd
root:x:::root:/root:/bin/bash
shutdown:x:::shutdown:/sbin:/sbin/shutdown
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
liujunjun:x::::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep -w ".\{0,2\}sh" /etc/passwd
root:x:::root:/root:/bin/bash
liujunjun:x::::/home/liujunjun:/bin/bash

位置锚定:

      ^ :锚定行首

      $ :锚定行尾。技巧:"^$"用于匹配空白行。

      \b或\<:锚定单词的词首。如"\blike"不会匹配alike,但是会匹配liker

      \b或\>:锚定单词的词尾。如"\blike\b"不会匹配alike和liker,只会匹配like

      \B :与\b作用相反。

[root@node2 scprits]# grep "h" /etc/passwd
root:x:::root:/root:/bin/bash
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
chrony:x::::/var/lib/chrony:/sbin/nologin
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
nginx:x::::/home/nginx:/sbin/nologin
liujunjun:x::::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "h$" /etc/passwd
root:x:::root:/root:/bin/bash
liujunjun:x::::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "\<sh" /etc/passwd
shutdown:x:::shutdown:/sbin:/sbin/shutdown
[root@node2 scprits]# grep "\Bsh\b" /etc/passwd
root:x:::root:/root:/bin/bash
liujunjun:x::::/home/liujunjun:/bin/bash

分组及引用:

      \(string\) :将string作为一个整体方便后面引用

        \1 :引用第1个左括号及其对应的右括号所匹配的内容。

        \2 :引用第2个左括号及其对应的右括号所匹配的内容。

        \n :引用第n个左括号及其对应的右括号所匹配的内容。

[root@node2 scprits]# grep "^\([[:alpha:]]\).*\1$" /etc/passwd
nobody:x:::Nobody:/:/sbin/nologin
ntp:x::::/etc/ntp:/sbin/nologin
nginx:x::::/home/nginx:/sbin/nologin

3、扩展的(Extend)正则表达式(注意要使用扩展的正则表达式要加-E选项,或者直接使用egrep):

    匹配字符:这部分和基本正则表达式一样

    匹配次数

      * :和基本正则表达式一样

      ? :基本正则表达式是\?,二这里没有\。

      {m,n} :相比基本正则表达式也是没有了\。

      + :匹配其前面的字符至少一次,相当于{1,}。

    位置锚定:和基本正则表达式一样。

    分组及引用

      (string) :相比基本正则表达式也是没有了\。

        \1 :引用部分和基本正则表达式一样。

        \n :引用部分和基本正则表达式一样。

    或者

      a|b :匹配a或b,注意a是指 | 的左边的整体,b也同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,则应该写为 (C|c)at 。记住(string)除了用于引用还用于分组。  

注1:默认情况下,正则表达式的匹配工作在贪婪模式下,也就是说它会尽可能长地去匹配,比如某一行有字符串 abacb,如果搜索内容为 "a.*b" 那么会直接匹配 abacb这个串,而不会只匹配ab或acb。

注2:所有的正则字符,如 [ 、* 、( 等,若要搜索 * ,而不是想把 * 解释为重复先前字符任意次,可以使用 \* 来转义。

下面用一个练习来结束本次grep的学习:

在网络配置文件 /etc/sysconfig/network-scripts/ifcfg-ens33 中检索出所有的 IP

1、检索出 0-255的范围

[root@node2 scprits]# egrep "[0-9]|[1-9]|[0-9]|1[0-9]|[0-9]|2[0-4]|[0-9]|25[0-5]|" /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=eb0d19d9-a7a0-458e-997d-19b01b9e4fe1
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.223
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@node2 scprits]# egrep "\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
> ([-]|[-][-]|[-][-]|[-][-]|[-])\.\
> ([-]|[-][-]|[-][-]|[-][-]|[-])\.\
> ([-]|[-][-]|[-][-]|[-][-]|[-])\b" \
> /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.1.223
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@node2 scprits]# egrep "(\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.\b){3}\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b" /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.1.223
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=8.8.8.8

最新文章

  1. 关闭显示器API及命令
  2. MVC4.0 扩展辅助方法
  3. [转]C#在创建完项目后如何重命名项目名称。
  4. 剑指Offer 矩形覆盖
  5. 倒计时,js
  6. color 的一些处理
  7. Java Day 15
  8. JMS - ConnectionMetaData
  9. 九度OJ 1512 用两个栈实现队列 【数据结构】
  10. 使用NSURLSession获取网络数据和下载文件
  11. Kooboo中主要的几个关键词中的关系
  12. win7下wubi安装Ubuntu,重装win7后找回Ubuntu启动项
  13. 转自http://blog.sina.com.cn/daylive——C++ STL set&amp;multiset
  14. 用SQL脚本移除视图中存在的机器名
  15. Thinkphp利用微信多客服消息推送取货二维码消息
  16. POJ 2373 Yogurt factory
  17. 介绍CSS的相关知识
  18. 在写php项目时 修改外部css或js文件没有效果
  19. js调试工具及微博登录分析
  20. JS分页条插件

热门文章

  1. 剑指Offer-17.树的子结构(C++/Java)
  2. luoguP4331 [BOI2004]Sequence 数字序列
  3. version_compare ()
  4. Nginx企业级优化
  5. ASP.NET开发实战——(六)ASP.NET MVC &amp; 分层 代码篇
  6. Cntlm 配置上网代理
  7. RoadMap:如何创建产品路线图
  8. 在 EF Core 中 Book 实体在新增、修改、删除时,给 LastUpdated 字段赋值。
  9. 使用角色管理工具 安装或配置microsoft.net framework 3.5 sp1
  10. Redis操作篇(二)