正则表达式

正则表达式概述

  • 正则表达式作为一个 pattern,将 pattern 与要搜索的字符串进行匹配,以便查找一个或多个字符串。
  • 正则表达式,自成体系,由普通字符(例如字符 a 到 z)和元字符组成的文字模式。
  • 普通字符:没有显式指定为元字符的所有可打印和不可打印字符字符,包括所有大写和小写字母、所有数字、所有标点符号和其他一些符号。
  • 元字符:除了普通字符之外的字符。
  • 正则表达式,工具(vim、grep、less等)和程序语言(Perl、Python、C等)都使用正则表达式。

正则表达式分类:

  • 普通正则表达式
  • 扩展正则表示,支持更多的元字符。

环境准备

[my@centos7 ~ 10:08:55]$ cat >words<<'EOF'
> cat
> category
> acat
> concatenate
> cbt
> c1t
> cCt
> c-t
> c.t
> dog
> EOF

用法:

[my@centos7 ~ 10:19:12]$ cat words | grep 'cat'
cat
category
acat
concatenate
[my@centos7 ~ 10:20:36]$ cat words | grep 'c.t'
cat
category
acat
concatenate
cbt
c1t
cCt
c-t
c.t
[my@centos7 ~ 10:20:45]$ cat words | grep 'c[ab]t'
cat
category
acat
concatenate
cbt
[my@centos7 ~ 10:20:57]$ cat words | grep 'c[a-z]t'
cat
category
acat
concatenate
cbt
[my@centos7 ~ 10:21:05]$ cat words | grep 'c[A-Z]t'
cCt
[my@centos7 ~ 10:21:21]$ cat words | grep 'c[0-9]t'
c1t
[my@centos7 ~ 10:21:29]$ cat words | grep 'c[a-z0-9]t'
cat
category
acat
concatenate
cbt
c1t
[my@centos7 ~ 10:22:20]$ cat words | grep 'c[a-zA-Z0-9]t'
cat
category
acat
concatenate
cbt
c1t
cCt
[my@centos7 ~ 10:23:13]$ cat words | grep 'c[-a-zA-Z0-9]t'
cat
category
acat
concatenate
cbt
c1t
cCt
c-t
[my@centos7 ~ 10:23:31]$ cat words | grep 'c[^ab]t'
cCt
c-t
c.t
[my@centos7 ~ 10:23:51]$ cat words | grep 'c[a^b]t'
cat
category
acat
concatenate
cbt
[my@centos7 ~ 10:24:02]$ cat words | grep 'c\.t'
c.t
[my@centos7 ~ 10:24:22]$ cat words | grep 'c\at'
cat
category
acat
concatenate
[my@centos7 ~ 10:24:33]$ cat words | grep 'cat|dog'
[my@centos7 ~ 10:25:00]$ cat words | grep -E 'cat|dog'
cat
category
acat
concatenate
dog

字符集总结

选项描述
[[:digit:]]数字: 0 1 2 3 4 5 6 7 8 9 等同于[0-9]
[[:xdigit:]]十六进制数字: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f 等同于[0-9a-fA-F]
[[:lower:]]小写字母:在 C 语言环境和ASCII字符编码中,对应于[a-z]
[[:upper:]]大写字母:在 C 语言环境和ASCII字符编码中,对应于[A-Z]
[[:alpha:]]字母字符:[[:lower:]和[[:upper:]];在C语言环境和ASCII字符编码中,等同于**[A-Za-z]**
[[:alnum:]]字母数字字符:[:alpha:]和[:digit:];在C语言环境和ASCII字符编码中,等同于**[0-9A-Za-z]**
[[:blank:]]或者[[:space:]]空白字符:在 C 语言环境中,它对应于制表符、换行符、垂直制表符、换页符、回车符和空格。
[[:punct:]]标点符号:在C语言环境和ASCII字符编码中,它对应于!" # $ % &'()*+,-./:;<=>?@[]^_`{|}~
[[:print:]]或者 [[:graph:]]可打印字符: [[:alnum:]]、[[:punct:]]。
[[:cntrl:]]控制字符。在 ASCII中, 这些字符对应八进制代码000到037和 177 (DEL)。

非打印字符

终端中不显示的字符,例如换行符。

字符描述
\cx匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 ‘c’ 字符。
\f匹配一个换页符。等价于 \x0c\cL
\n匹配一个换行符。等价于 \x0a\cJ
\r匹配一个回车符。等价于 \x0d\cM
\s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。注意 Unicode 正则表达式会匹配全角空格符。
\S匹配任何**空白字符**。等价于 [^ \f\n\r\t\v]
\w匹配字母、数字、下划线。等价于 [A-Za-z0-9_]
\W匹配任何非单词字符。等价于[^A-Za-z0-9_]
\t匹配一个制表符。等价于 \x09\cI
\v匹配一个垂直制表符。等价于 \x0b\cK

grep 命令支持\w\W\s\S

定位符

^

匹配行首位置。

[my@centos7 ~ 10:25:23]$ cat words | grep '^cat'
cat
category

$

匹配行末位置。

[my@centos7 ~ 11:53:42]$ cat words | grep 'cat$'
cat
acat

综合练习

[my@centos7 ~ 12:43:03]$ cat words | grep '^cat$'
cat
[my@centos7 ~ 12:43:18]$ cat /etc/profile | egrep  '^[^#]'
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
unset i
unset -f pathmunge
[my@centos7 ~ 13:00:52]$ cat /etc/profile | egrep -v '^#|^$'
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
unset i
unset -f pathmunge
[my@centos7 ~ 13:01:15]$ yum-config-manager | grep '^\[' | grep -v main
[base]
[epel]
[extras]
[updates]

\b

[my@centos7 ~ 13:22:58]$ echo hello cat kitty >> words 
[my@centos7 ~ 13:34:41]$ cat words | grep '\bcat'
cat
category
hello cat kitty
[my@centos7 ~ 13:34:47]$ cat words | grep 'cat\b'
cat
acat
hello cat kitty
[my@centos7 ~ 13:34:54]$ cat words | grep '\bcat\b'
cat
hello cat kitty

基本不用的三个grep用法

[my@centos7 ~ 14:15:25]$ cat words | grep '\Bcat\B'
concatenate
[my@centos7 ~ 14:15:38]$ cat words | grep '\<cat'
cat
category
hello cat kitty
[my@centos7 ~ 14:16:09]$ cat words | grep 'cat\>'
cat
acat
hello cat kitty

限定次数

[my@centos7 ~ 14:16:29]$ echo dg >>words
[my@centos7 ~ 14:19:12]$ echo doog >>words
[my@centos7 ~ 14:19:20]$ cat words | grep 'do*g'
dog
dg
doog
[my@centos7 ~ 14:19:40]$ cat words | egrep 'do+g'
dog
doog
[my@centos7 ~ 14:20:41]$ cat words | egrep 'do?g'
dog
dg
[my@centos7 ~ 14:20:56]$ cat words | egrep 'do{2}g'
doog
[my@centos7 ~ 14:21:07]$ echo dooog >>words
[my@centos7 ~ 14:21:42]$ echo doooog >>words
[my@centos7 ~ 14:21:59]$ cat words | egrep 'do{2,3}g'
[my@centos7 ~ 14:22:36]$ cat words
cat
category
acat
concatenate
cbt
clt
cCt
c-t
c.t
dog
hello cat kitty
dg
doog
dooog
doooog
[my@centos7 ~ 14:22:43]$ cat words | egrep 'do{2,3}g'
doog
dooog
[my@centos7 ~ 14:23:04]$ cat words | egrep 'do{2,}g'
doog
dooog
doooog
[my@centos7 ~ 14:23:14]$ cat words | egrep 'do{,3}g'
dog
dg
doog
dooog
[my@centos7 ~ 14:23:30]$ echo dogdog >>words
[my@centos7 ~ 14:23:59]$ echo dogdogdog >>words
[my@centos7 ~ 14:24:08]$ echo dogdogdogdog >>words
[my@centos7 ~ 14:24:14]$ cat words | egrep '(dog){2,3}'
dogdog
dogdogdog
dogdogdogdog
[my@centos7 ~ 14:24:44]$ cat words | egrep '(dog){2,}'
dogdog
dogdogdog
dogdogdogdog

综合案例

综合案例
如何过滤出以下内容中所有有效IPv4地址?

0.0.0.0
1.1.1.1
11.11.11.111
111.111.111.111
999.9.9.9
01.1.1.1
10.0.0.0
0.1.1.1
266.1.1.1
248.1.1.1
256.1.1.1

结果

[my@centos7 ~ 14:24:56]$ cat >ip<<'EOF'
> 0.0.0.0
> 1.1.1.1
> 11.11.11.111
> 111.111.111.111
> 999.9.9.9
> 01.1.1.1
> 10.0.0.0
> 0.1.1.1
> 266.1.1.1
> 248.1.1.1
> 256.1.1.1
> EOF
[my@centos7 ~ 14:29:26]$ cat ip | egrep '\b(([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))(\.(([1-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))){3}\b'
1.1.1.1
11.11.11.111
111.111.111.111
10.0.0.0
248.1.1.1

grep用法

[my@centos7 ~ 14:42:20]$ cat words | egrep '(dog){3}'
dogdogdog
dogdogdogdog
[my@centos7 ~ 15:03:47]$ cat words | egrep 'cat|dog'
cat
category
acat
concatenate
dog
hello cat kitty
dogdog
dogdogdog
dogdogdogdog
[my@centos7 ~ 15:04:04]$ echo -e 'cat\ndog'>pattens_file
[my@centos7 ~ 15:04:49]$ cat pattens_file
cat
dog
[my@centos7 ~ 15:05:03]$ cat words |grep -f pattens_file
cat
category
acat
concatenate
dog
hello cat kitty
dogdog
dogdogdog
dogdogdogdog
[my@centos7 ~ 15:05:21]$ cat words | -i 'cBt'
bash: -i: 未找到命令...
[my@centos7 ~ 15:05:53]$ cat words | grep -i 'cBt'
cbt
[my@centos7 ~ 15:06:03]$ cat words | grep '\bcat\b'
cat
hello cat kitty
[my@centos7 ~ 15:07:16]$ cat words | grep -x 'cat'
cat
[my@centos7 ~ 15:07:34]$ cat words | egrep -v '^d|^c'
acat
hello cat kitty
[my@centos7 ~ 15:08:04]$ egrep -v '^\s*#|^$' /etc/profile
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
unset i
unset -f pathmunge
[my@centos7 ~ 15:09:19]$ cat words | grep 'dog'
dog
dogdog
dogdogdog
dogdogdogdog
[my@centos7 ~ 15:09:35]$ cat words | grep -m2 'dog'
dog
dogdog
[my@centos7 ~ 15:09:49]$ cat words | grep -c 'dog'
4
[my@centos7 ~ 15:09:59]$ head -5 words
cat
category
acat
concatenate
cbt
[my@centos7 ~ 15:10:07]$ cat words | grep -b 'cat'
0:cat
4:category
13:acat
18:concatenate
54:hello cat kitty
[my@centos7 ~ 15:10:22]$ cat words | grep -n 'cat'
1:cat
2:category
3:acat
4:concatenate
11:hello cat kitty
[my@centos7 ~ 15:10:30]$ cat words | egrep '(dog){3}'
dogdogdog
dogdogdogdog
[my@centos7 ~ 15:11:10]$ cat words | egrep -o '(dog){3}'
dogdogdog
dogdogdog
[my@centos7 ~ 15:11:21]$ cat words | egrep -q '(dog){3}'
[my@centos7 ~ 15:11:31]$ echo $?
0
[my@centos7 ~ 15:11:39]$ cat words | egrep -q '(dog){3}asdfsfdasf'
[my@centos7 ~ 15:12:02]$ echo $?
1
[my@centos7 ~ 15:12:04]$ grep '^SELINUX=' /etc/shadow /etc/selinux/config
grep: /etc/shadow: 权限不够
/etc/selinux/config:SELINUX=disabled
[my@centos7 ~ 15:12:54]$ grep -s '^SELINUX=' /etc/shadow /etc/selinux/config
/etc/selinux/config:SELINUX=disabled

grep在IP的应用

[root@centos7 ~ 16:01:38]# ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
ens33            UP             10.1.8.10/24 fe80::20c:29ff:fecd:61ee/64 
virbr0           DOWN           192.168.122.1/24 
virbr0-nic       DOWN           
[root@centos7 ~ 16:01:48]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:61ee/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:c8:76:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:c8:76:7a brd ff:ff:ff:ff:ff:ff
[root@centos7 ~ 16:04:14]# ip a | grep 10.1.8.10
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:04:24]# ip a | grep -B1 10.1.8.10
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:04:52]# ip a | grep -B1 10.1.8.10|head -1
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
[root@centos7 ~ 16:05:45]# ip a |grep -B1 10.1.8.10 | head -1 | awk '{print $2}'
00:0c:29:cd:61:ee
[root@centos7 ~ 16:06:22]# ip a | grep -B1 10.1.8.10
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:07:17]# ip a | grep 10.1.8.10
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:07:31]# ip a | grep 10.1.8.10 | awk '{print $NF}'
ens33
[root@centos7 ~ 16:08:37]# ip a |grep -B1 10.1.8.10 | head -1 | awk '{print $2}'
00:0c:29:cd:61:ee
[root@centos7 ~ 16:08:51]# ip a | grep ens33 -A3
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:61ee/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos7 ~ 16:09:19]# ip a | grep ens33 -A2
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:61ee/64 scope link 
[root@centos7 ~ 16:09:23]# ip a | grep ens33: -A2
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:09:30]# ip a | grep 'ens33$'
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
[root@centos7 ~ 16:09:56]# ip a | grep 'ens33$' | awk '{print$2}'
10.1.8.10/24
[root@centos7 ~ 16:10:23]# ip a | grep -C2 10.1.8.10
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:61:ee brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.10/24 brd 10.1.8.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:61ee/64 scope link 

sed 工作流程

读取行 -> 执行 -> 显示 -> 读取行 -> 执行 -> 显示 -> .... -> 读取行 -> 执行 -> 显示

image-20260414163244092

  1. 读取行

sed 从输入流 (文件、管道、标准输入流)中读取 一行 并存储在名叫 pattern space 的内部空间中。

sed 是行文字处理器。每次只会读取一行。

sed 内部会有一个计数器,记录着当前已经处理多少行,也就是当前行的行号。

  1. 执行

按照 sed 命令定义的顺序依次应用于刚刚读取的 一行 数据。

默认情况下,sed 一行一行的处理所有的输入数据。但如果我们指定了行号,则只会处理指定的行。

  1. 显示

把经过 sed 命令处理的数据发送到输出流(文件、管道、标准输出),并同时清空 pattern space 空间。

  1. 上面流程一直循环,直到输入流中的数据全部处理完成。

sed 注意事项

整个流程看似简单,有几个知识点需要注意:

  • pattern space 空间是 sed 在内存中开辟的一个私有的存储区域。内存的特性,会导致关闭命令行或关机数据就没了。
  • 默认情况下,sed 命令只会处理 pattern space 空间中的数据,且并不会将处理后的数据保存到源文件中。也就是说,sed 默认并不会修改源文件。

但 GNU SED 提供提供了一种方式用于修改 源文件。方式就是传递 -i 选项,在后面的章节中介绍。

  • sed 还在内存上开辟了另一个私有的空间 hold space 用于保存处理后的数据以供以后检索。

每一个周期执行结束,sed 会清空 pattern space 空间的内容,但 hold space 空间的内容并不会清空。hold space 空间用于存储处理后数据,sed 命令并不会对这里的数据处理。

这样,当 sed 需要之前处理后的数据时,可以随时从 hold space 空间读取。

  • sed 程序执行前,模式 patternhold space 空间都是空的。
  • 如果我们没有传递任何输入文件,sed 默认会从 标准输入 中读取数据。
  • sed 可以指定只处理输入数据中的行范围。默认情况下是全部行,因此会依次处理每一行。

sed 命令语法

总的来说 sed 命令主要由四部分构成:

  1. sed 命令。
  2. [option] 命令行选项,用于改变 sed 的工作流程。
  3. [sed-command] 是具体的 sed 命令。
  4. [input-file] 输入数据,如果不指定,则默认从标准输入中读取。
[root@centos7 ~ 16:15:26]# cat >data.txt<<'EOF'
> I am studing sed
> I am www.laoma.cloud
> I am a Superman
> I am so handsome
> EOF
[root@centos7 ~ 17:06:09]# sed '' data.txt
I am studing sed
I am www.laoma.cloud
I am a Superman
I am so handsome
[root@centos7 ~ 17:06:50]# sed ''
hello world
hello world
[root@centos7 ~ 17:07:02]# sed -e '' data.txt
I am studing sed
I am www.laoma.cloud
I am a Superman
I am so handsome
[root@centos7 ~ 17:07:22]# sed -e '1d' -e '2d' -e '5d' data.txt
I am a Superman
I am so handsome
[root@centos7 ~ 17:07:57]# sed -e '1d;2d;5d' data.txt
I am a Superman
I am so handsome
[root@centos7 ~ 17:08:20]# echo -e "1d\n2d\n5d">scripts
[root@centos7 ~ 17:08:59]# cat scripts
1d
2d
5d
[root@centos7 ~ 17:09:05]# sed -f scripts data.txt
I am a Superman
I am so handsome
[root@centos7 ~ 17:09:19]# sed -n '' data.txt 
[root@centos7 ~ 17:09:39]# sed -n '1p' data.txt 
I am studing sed
[root@centos7 ~ 17:09:51]# echo 'This is 1
> This is 2
> This is 3
> This is 4
> This is 5'
This is 1
This is 2
This is 3
This is 4
This is 5
[root@centos7 ~ 17:10:48]# echo 'This is 1    
> This is 2    
> This is 3
> This is 4
> This is 5 ' > test
[root@centos7 ~ 17:11:37]# cat test | sed ''
This is 1    
This is 2    
This is 3
This is 4
This is 5 
[root@centos7 ~ 17:13:04]# cat test | sed -n 'p'
This is 1    
This is 2    
This is 3
This is 4
This is 5 
[root@centos7 ~ 17:13:24]# cat test | sed -n '1p'
This is 1    
[root@centos7 ~ 17:13:36]# cat test | sed -n '$p'
This is 5 
[root@centos7 ~ 17:13:45]# cat test | sed -n '1,3p'
This is 1    
This is 2    
This is 3
[root@centos7 ~ 17:13:57]# cat test | sed -n '3,$p'
This is 3
This is 4
This is 5 
[root@centos7 ~ 17:14:10]# cat test | sed -n '2,+2p'
This is 2    
This is 3
This is 4
[root@centos7 ~ 17:14:33]# cat test | sed -n '1~2p'
This is 1    
This is 3
This is 5 
[root@centos7 ~ 17:15:01]# cat << 'EOF' > ~/test
> root:x:0:0:root:/root:/bin/bash
> bin:x:1:1:bin:/bin:/bin/false
> daemon:x:2:2:daemon:/sbin:/bin/false
> mail:x:8:12:mail:/var/spool/mail:/bin/false
> ftp:x:14:11:ftp:/home/ftp:/bin/false
> &nobody:$:99:99:nobody:/:/bin/false
> zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
> http:x:33:33::/srv/http:/bin/false
> dbus:x:81:81:System message bus:/:/bin/false
> hal:x:82:82:HAL daemon:/:/bin/false
> mysql:x:89:89::/var/lib/mysql:/bin/false
> aaa:x:1001:1001::/home/aaa:/bin/bash
> ba:x:1002:1002::/home/zhangy:/bin/bash
> test:x:1003:1003::/home/test:/bin/bash
> @zhangying:*:1004:1004::/home/test:/bin/bash
> policykit:x:102:1005:Po
> EOF
[root@centos7 ~ 17:15:26]# cat test | sed -n '/zhang/p'
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
[root@centos7 ~ 17:15:56]# cat test | sed -n '/^root/,/mail/p'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
[root@centos7 ~ 17:16:42]# cat test | sed -n '/^root/,3p'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
[root@centos7 ~ 17:16:59]# cat test | sed -n '/^root/,$p'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:18:22]# echo 'This is 1    
> This is 2    
> This is 3' | sed -n '1{N;p}'
This is 1    
This is 2    
[root@centos7 ~ 17:18:30]# echo 'This is 1    
This is 2    
This is 3' | sed -n '1{N;P}'
This is 1    
[root@centos7 ~ 17:20:46]# echo 'This is 1
> This is 2
> This is 3
> This is 4
> This is 5' | sed -n 'n;p'
This is 2
This is 4
[root@centos7 ~ 17:21:14]# cat test | sed 'N;s/\n/==/'
root:x:0:0:root:/root:/bin/bash==bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false==mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false==&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash==http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false==hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false==aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash==test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash==policykit:x:102:1005:Po
[root@centos7 ~ 17:22:15]# echo 'This is 1    
> This is 2    
> This is 3    
> This is 4    
> This is 5' | sed -n '1{N;p}'
This is 1    
This is 2    
[root@centos7 ~ 17:22:31]# sed 's/root/tankzhang/' test | grep tankzhang
tankzhang:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 17:23:16]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' config
sed: can't read config: No such file or directory
[root@centos7 ~ 17:24:04]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' config
sed: can't read config: No such file or directory
[root@centos7 ~ 17:24:47]# sed 's/root/tankzhang/g' test | grep tankzhang
tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
[root@centos7 ~ 17:28:15]# sed 's/root/tankzhang/p' test 
tankzhang:x:0:0:root:/root:/bin/bash
tankzhang:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:28:34]# sed -n 's/root/tankzhang/p' test 
tankzhang:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 17:28:55]# sed -n 's/root/tankzhang/gp' test 
tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
[root@centos7 ~ 17:29:10]# sed -ne '2,8s/^zhang/ying/gp' test 
yingy:x:1000:100:,,,:/home/zhangy:/bin/bash
[root@centos7 ~ 17:29:48]# sed -ne '/^zhang/,/Po/ s/zhang/ying/gp' test 
yingy:x:1000:100:,,,:/home/yingy:/bin/bash
ba:x:1002:1002::/home/yingy:/bin/bash
@yingying:*:1004:1004::/home/test:/bin/bash
[root@centos7 ~ 17:30:29]# sed -n 's#root#hello#gp' test
hello:x:0:0:hello:/hello:/bin/bash
[root@centos7 ~ 17:31:18]# cat test1.txt
cat: test1.txt: No such file or directory
[root@centos7 ~ 17:31:27]# vim test1.txt
[root@centos7 ~ 17:32:41]# sed "s/myyour/3" test1.txt
sed: -e expression #1, char 10: unterminated `s' command
[root@centos7 ~ 17:34:15]# sed "s/my/your/3" test1.txt
my my your my my
my my your my my
my my your my my
[root@centos7 ~ 17:34:24]# sed "3s/my/your/1" test1.txt
my my my my my
my my my my my
your my my my my
[root@centos7 ~ 17:34:38]# sed "s/my/your/3g" test1.txt
my my your your your
my my your your your
my my your your your
[root@centos7 ~ 17:35:08]# sed "s/my/your/;s/my/your/" test1.txt
your your my my my
your your my my my
your your my my my
[root@centos7 ~ 17:36:17]# sed "s/my/your/2;s/my/your/2;s/my/your/2" test1.txt
my your your your my
my your your your my
my your your your my
[root@centos7 ~ 17:37:30]# cat test | sed -n '2,8s/^zhang/ying/gp;5,10s#dbus#goodbay#gp'
yingy:x:1000:100:,,,:/home/zhangy:/bin/bash
goodbay:x:81:81:System message bus:/:/bin/false
[root@centos7 ~ 17:37:57]# cat test | sed -ne '2,8s/zhang/ying/gp' -ne '5,10s#dbus#goodbay#gp'
yingy:x:1000:100:,,,:/home/yingy:/bin/bash
goodbay:x:81:81:System message bus:/:/bin/false
[root@centos7 ~ 17:38:07]# sed '/root/a====aaaa====' test
root:x:0:0:root:/root:/bin/bash
====aaaa====
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:38:29]# sed '/root/i====iiii====' test
====iiii====
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:39:24]# sed -nr 's/^root(.*)/root\1\n====aaaa====/p' test root:x:0:0:root:/root:/bin/bash ====aaaa====
root:x:0:0:root:/root:/bin/bash
====aaaa====
sed: can't read root:x:0:0:root:/root:/bin/bash: No such file or directory
sed: can't read ====aaaa====: No such file or directory
[root@centos7 ~ 17:39:37]# sed -nr 's/^root(.*)/root\1\n====aaaa====/p' test
root:x:0:0:root:/root:/bin/bash
====aaaa====
[root@centos7 ~ 17:40:05]# sed -nr 's/^root(.*)/====iiii====\nroot\1/p' test
====iiii====
root:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 17:40:56]# sed -nr 's/^root/haharoot/p' test
haharoot:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 17:42:02]# sed -nr 's/^root(.*)/root\1heihei/p' test
root:x:0:0:root:/root:/bin/bashheihei
[root@centos7 ~ 17:42:43]# sed -nr 's/root/my-root/gp' test
my-root:x:0:0:my-root:/my-root:/bin/bash
[root@centos7 ~ 17:43:24]# sed -nr 's/root/root-my/gp' test
root-my:x:0:0:root-my:/root-my:/bin/bash
[root@centos7 ~ 17:44:03]# sed -e '1,14d' test
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:45:22]# sed -e '4,$d' test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
[root@centos7 ~ 17:45:42]# sed -re '/(false|bash)/d' test
policykit:x:102:1005:Po
[root@centos7 ~ 17:46:24]# sed -e '/root/,/^test/d' test
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:47:05]# sed -r 's/.*root.*$//;/^$/d' test
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 17:48:26]# sed -nr 's/^root//gp' test :x:0:0:root:/root:/bin/bash
:x:0:0:root:/root:/bin/bash
sed: can't read :x:0:0:root:/root:/bin/bash: No such file or directory
[root@centos7 ~ 17:56:53]# echo 'This is 1    
> This is 2    
> This is 3    
> This is 4    
> This is 5' | sed 'N;D'
This is 5
[root@centos7 ~ 17:57:29]# echo 'This is 1    
> This is 2    
> This is 3    
> This is 4    
> This is 5' | sed 'n;D'
This is 1    
This is 3    
This is 5
[root@centos7 ~ 17:57:47]# sed '=' test
1
root:x:0:0:root:/root:/bin/bash
2
bin:x:1:1:bin:/bin:/bin/false
3
daemon:x:2:2:daemon:/sbin:/bin/false
4
mail:x:8:12:mail:/var/spool/mail:/bin/false
5
ftp:x:14:11:ftp:/home/ftp:/bin/false
6
&nobody:$:99:99:nobody:/:/bin/false
7
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
8
http:x:33:33::/srv/http:/bin/false
9
dbus:x:81:81:System message bus:/:/bin/false
10
hal:x:82:82:HAL daemon:/:/bin/false
11
mysql:x:89:89::/var/lib/mysql:/bin/false
12
aaa:x:1001:1001::/home/aaa:/bin/bash
13
ba:x:1002:1002::/home/zhangy:/bin/bash
14
test:x:1003:1003::/home/test:/bin/bash
15
@zhangying:*:1004:1004::/home/test:/bin/bash
16
policykit:x:102:1005:Po
[root@centos7 ~ 17:58:22]# sed '=' test | sed 'N;s/\n/:/'
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/bin/false
3:daemon:x:2:2:daemon:/sbin:/bin/false
4:mail:x:8:12:mail:/var/spool/mail:/bin/false
5:ftp:x:14:11:ftp:/home/ftp:/bin/false
6:&nobody:$:99:99:nobody:/:/bin/false
7:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
8:http:x:33:33::/srv/http:/bin/false
9:dbus:x:81:81:System message bus:/:/bin/false
10:hal:x:82:82:HAL daemon:/:/bin/false
11:mysql:x:89:89::/var/lib/mysql:/bin/false
12:aaa:x:1001:1001::/home/aaa:/bin/bash
13:ba:x:1002:1002::/home/zhangy:/bin/bash
14:test:x:1003:1003::/home/test:/bin/bash
15:@zhangying:*:1004:1004::/home/test:/bin/bash
16:policykit:x:102:1005:Po
[root@centos7 ~ 17:59:12]# echo 'This is 1    
> This is 2    
> This is 3    
> This is 4    
> This is 5' | sed 'y/si/SI/'
ThIS IS 1    
ThIS IS 2    
ThIS IS 3    
ThIS IS 4    
ThIS IS 5
[root@centos7 ~ 18:00:04]# vim test2
[root@centos7 ~ 18:00:54]# sed -e '/^root/r test2' test
root:x:0:0:root:/root:/bin/bash
=============
-------------
+++++++++++++
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 18:02:18]# sed -n '/^root/w test3 test
> ^C
[root@centos7 ~ 18:03:18]# sed -n '/^root/w test3' test
[root@centos7 ~ 18:03:26]# cat test3
root:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 18:03:32]# vim scripts
[root@centos7 ~ 18:05:22]# sed -n -f scripts test
[root@centos7 ~ 18:05:36]# cat write.log
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
[root@centos7 ~ 18:05:58]# vim scripts
[root@centos7 ~ 18:07:02]# sed -n -f scripts test
[root@centos7 ~ 18:07:14]# cat write.log
root:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 18:07:21]# sed '/^root/chello' test
hello
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 18:08:15]# echo 'This is 1    
> This is 2    
> This is 3' | sed -n '1{N;p}'
This is 1    
This is 2    
[root@centos7 ~ 18:08:59]# sed -n '1,5{s/^root/hello/;p}' test
hello:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
[root@centos7 ~ 18:09:08]# vim scripts
[root@centos7 ~ 18:09:40]# sed -n -f scripts test
[root@centos7 ~ 18:09:59]# cat write.log
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
[root@centos7 ~ 18:10:04]# sed '2q' test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
[root@centos7 ~ 18:10:19]# sed -e 's/bin/tank/g:3q' test
sed: -e expression #1, char 13: unknown option to `s'
[root@centos7 ~ 18:11:00]# sed -e 's/bin/tank/g;3q' test
root:x:0:0:root:/root:/tank/bash
tank:x:1:1:tank:/tank:/tank/false
daemon:x:2:2:daemon:/stank:/tank/false
[root@centos7 ~ 18:11:49]# sed -e '/root/h' -e '$G' test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
root:x:0:0:root:/root:/bin/bash
[root@centos7 ~ 18:12:13]# sed -e '/root/h' -e '/zhangy/g' test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
root:x:0:0:root:/root:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
root:x:0:0:root:/root:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
root:x:0:0:root:/root:/bin/bash
policykit:x:102:1005:Po
[root@centos7 ~ 18:12:33]# sed -ne '2,8s/^\(zhangy\)/zhangying/gp' test
zhangying:x:1000:100:,,,:/home/zhangy:/bin/bash
[root@centos7 ~ 18:12:51]# sed -nre '2,8s/^(zhangy)/zhangying/gp' test
zhangying:x:1000:100:,,,:/home/zhangy:/bin/bash
[root@centos7 ~ 18:13:36]# egrep 'ba.*zhang' test
ba:x:1002:1002::/home/zhangy:/bin/bash
[root@centos7 ~ 18:13:58]# sed -nre 's/(ba).*(zhangy)/\1/gp' test
ba:/bin/bash
[root@centos7 ~ 18:15:15]# sed -nre 's/(ba).*(zhangy)/&,haha/gp' test
ba:x:1002:1002::/home/zhangy,haha:/bin/bash
[root@centos7 ~ 18:15:50]# sed 'G' test
root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/bin/false

daemon:x:2:2:daemon:/sbin:/bin/false

mail:x:8:12:mail:/var/spool/mail:/bin/false

ftp:x:14:11:ftp:/home/ftp:/bin/false

&nobody:$:99:99:nobody:/:/bin/false

zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash

http:x:33:33::/srv/http:/bin/false

dbus:x:81:81:System message bus:/:/bin/false

hal:x:82:82:HAL daemon:/:/bin/false

mysql:x:89:89::/var/lib/mysql:/bin/false

aaa:x:1001:1001::/home/aaa:/bin/bash

ba:x:1002:1002::/home/zhangy:/bin/bash

test:x:1003:1003::/home/test:/bin/bash

@zhangying:*:1004:1004::/home/test:/bin/bash

policykit:x:102:1005:Po

licykit❌102:1005:Po
[root@centos7 ~ 18:12:33]# sed -ne ‘2,8s/^(zhangy)/zhangying/gp’ test
zhangying❌1000💯,:/home/zhangy:/bin/bash
[root@centos7 ~ 18:12:51]# sed -nre ‘2,8s/^(zhangy)/zhangying/gp’ test
zhangying❌1000💯,:/home/zhangy:/bin/bash
[root@centos7 ~ 18:13:36]# egrep ‘ba.zhang’ test
ba❌1002:1002::/home/zhangy:/bin/bash
[root@centos7 ~ 18:13:58]# sed -nre 's/(ba).
(zhangy)/\1/gp’ test
ba:/bin/bash
[root@centos7 ~ 18:15:15]# sed -nre ‘s/(ba).*(zhangy)/&,haha/gp’ test
ba❌1002:1002::/home/zhangy,haha:/bin/bash
[root@centos7 ~ 18:15:50]# sed ‘G’ test
root❌0:0:root:/root:/bin/bash

bin❌1:1:bin:/bin:/bin/false

daemon❌2:2:daemon:/sbin:/bin/false

mail❌8:12:mail:/var/spool/mail:/bin/false

ftp❌14:11:ftp:/home/ftp:/bin/false

&nobody:$:99:99:nobody:/:/bin/false

zhangy❌1000💯,:/home/zhangy:/bin/bash

http❌33:33::/srv/http:/bin/false

dbus❌81:81:System message bus:/:/bin/false

hal❌82:82:HAL daemon:/:/bin/false

mysql❌89:89::/var/lib/mysql:/bin/false

aaa❌1001:1001::/home/aaa:/bin/bash

ba❌1002:1002::/home/zhangy:/bin/bash

test❌1003:1003::/home/test:/bin/bash

@zhangying:*:1004:1004::/home/test:/bin/bash

policykit❌102:1005:Po


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐