sed-grep命令

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:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@node2 ~]# grep -i "Root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@node2 ~]# grep -in "Root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@node2 ~]# grep -ino "Root" /etc/passwd
1:root
1:root
1:root
10:root
[root@node2 ~]# grep -vc "root" /etc/passwd
22
[root@node2 ~]# cat /etc/passwd |wc -l
24
[root@node2 ~]# grep -A 2 "core id" /proc/cpuinfo 
core id         : 0
cpu cores       : 1
apicid          : 0
[root@node2 scprits]# cat > demo.c << eof
> #inclued <stdio.sh>
> int main ()
> {
> int hello=3
> int world=4;
> int helloworld=hello+world
> printf ("&d\n", helloworld);
> return;
> }
> eof
[root@node2 scprits]# grep "hello" demo.c 
int hello=3
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=3
[root@node2 scprits]# grep "hell[a-z]" demo.c 
int hello=3
int helloWorld=hello+world
printf ("&d\n", helloWorld);
[root@node2 scprits]# grep "hell[a-z][[:punct:]]" demo.c 
int hello=3
int helloWorld=hello+world

匹配次数:

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

[root@node2 scprits]# grep "/.*sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
liujunjun:x:1000:1000::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "/.\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
liujunjun:x:1000:1000::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep  -w ".\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
liujunjun:x:1000:1000::/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:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
nginx:x:997:995::/home/nginx:/sbin/nologin
liujunjun:x:1000:1000::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "h$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
liujunjun:x:1000:1000::/home/liujunjun:/bin/bash
[root@node2 scprits]# grep "\<sh" /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@node2 scprits]# grep "\Bsh\b" /etc/passwd
root:x:0:0:root:/root:/bin/bash
liujunjun:x:1000:1000::/home/liujunjun:/bin/bash

分组及引用:

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

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

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

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

[root@node2 scprits]# grep "^\([[:alpha:]]\).*\1$" /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
nginx:x:997:995::/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])\.\
> ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
> ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
> ([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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-03 13:46  星火撩原  阅读(742)  评论(0编辑  收藏  举报