shell中文本处理三剑客---之---sed命令详解

1.sed文本编辑器

我们知道,Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入、删除或替换数据中的文本。但 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据

sed 会根据脚本命令来处理文本文件中的数据,这些数据要么从命令行中输入,要么存储在一个文本文件中,此命令执行数据的顺序如下:

  1. 每次仅读取一行内容;
  2. 根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;
  3. 将执行结果输出。

当一行数据匹配完成后,它会继续读取下一行数据,并重复这个过程,直到将文件中所有数据处理完毕

注意:默认情况下sed命令不会修改原文件

sed 命令的基本格式如下:

sed [选项] [脚本命令] 文件名

sed 命令常用选项及含义:

选项 含义
-e 脚本命令 该选项会将其后跟的脚本命令添加到已有的命令中。
-f 脚本命令文件 该选项会将其后文件中的脚本命令添加到已有的命令中
-n 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。
-i 此选项会直接修改源文件,要慎用。

2.sed替换脚本命令

  1. sed s 替换脚本命令
    此命令的基本格式为:
[address]s/pattern/replacement/flags

其中,address 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容,此命令中常用的 flags 标记如下表所示:

flags 标记 功能
n 1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记;
g 对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A;
p 会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。
w file 将缓冲区中的内容写到指定的 file 文件中;
& 用正则表达式匹配的内容进行替换;
\n 匹配第 n 个子串,该子串之前在 pattern 中用 () 指定。
\ 转义(转义替换部分包含:&、\ 等)。

示例1: 指定 sed 用新文本替换第几处模式匹配的地方

可以看到,使用数字 2 作为标记的结果就是,sed 编辑器只替换每行中第 2 次出现的匹配模式

[root@server1 ~]# sed 's/Tue/Tua/2' date.txt 
Tue Tua Dec 17 15:40:54 CST 2019
Tue Tua Dec 17 15:40:57 CST 2019
Tue Tua Dec 17 15:40:57 CST 2019
Tue Dec 17 15:40:59 CST 2019
Tue Dec 17 15:41:00 CST 2019
Tue Dec 17 15:41:01 CST 2019
[root@server1 ~]# cat date.txt 
Tue Tue Dec 17 15:40:54 CST 2019
Tue Tue Dec 17 15:40:57 CST 2019
Tue Tue Dec 17 15:40:57 CST 2019
Tue Dec 17 15:40:59 CST 2019
Tue Dec 17 15:41:00 CST 2019
Tue Dec 17 15:41:01 CST 2019

示例2: 如果要用新文件替换所有匹配的字符串,可以使用 g 标记

[root@server1 ~]# cat date.txt 
Tue Dec 17 15:40:54 CST 2019
Tue Dec 17 15:40:57 CST 2019
Tue Dec 17 15:40:57 CST 2019
Tue Dec 17 15:40:59 CST 2019
Tue Dec 17 15:41:00 CST 2019
Tue Dec 17 15:41:01 CST 2019
[root@server1 ~]# sed 's/Tue/Tua/g' date.txt 
Tua Dec 17 15:40:54 CST 2019
Tua Dec 17 15:40:57 CST 2019
Tua Dec 17 15:40:57 CST 2019
Tua Dec 17 15:40:59 CST 2019
Tua Dec 17 15:41:00 CST 2019
Tua Dec 17 15:41:01 CST 2019

示例3: w 标记会将匹配后的结果保存到指定文件中,比如:

[root@server1 ~]# sed 's/Tue/Tua/w date2.txt' date.txt 
Tua Tue Dec 17 15:40:54 CST 2019
Tua Tue Dec 17 15:40:57 CST 2019
Tua Tue Dec 17 15:40:57 CST 2019
Tua Dec 17 15:40:59 CST 2019
Tua Dec 17 15:41:00 CST 2019
Tua Dec 17 15:41:01 CST 2019
[root@server1 ~]# cat date2.txt 
Tua Tue Dec 17 15:40:54 CST 2019
Tua Tue Dec 17 15:40:57 CST 2019
Tua Tue Dec 17 15:40:57 CST 2019
Tua Dec 17 15:40:59 CST 2019
Tua Dec 17 15:41:00 CST 2019
Tua Dec 17 15:41:01 CST 2019

示例4: 我们知道,-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行,例如:

[root@server1 ~]#  sed -n 's/test/trial/p' data3.txt
This is a trial line.
[root@server1 ~]# cat data3.txt 
This is a test line.
This is a different line.

示例5: 在使用 s 脚本命令时,替换类似文件路径的字符串会比较麻烦,需要将路径中的正斜线进行转义,例如:

[root@server1 ~]# sed 's/\/bin\/bash/\/bin\/csh/' /tmp/passwd 
root:x:0:0:root:/root:/bin/csh
Root:x:0:0:Root:/Root:/bin/csh
ROOT:x:0:0:ROOT:/ROOT:/bin/csh
bin:x:1:1:bin:/bin:/sbin/nologin
......
  1. sed d 替换脚本命令
    此命令的基本格式为:
[address]d

示例1: 如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。但使用该命令时要特别小心,如果你忘记指定具体行的话,文件中的所有内容都会被删除,什么也不会输出

[root@server1 ~]# cat data4.txt 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
[root@localhost ~]# sed 'd' data4.txt
#什么也不输出

示例2: 通过行号指定,比如删除 data6.txt 文件内容中的第 3 行:

[root@server1 ~]# cat data6.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@server1 ~]# sed '3d' data6.txt
This is line number 1.
This is line number 2.
This is line number 4.
[root@server1 ~]# cat data6.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

示例3: 或者通过特定行区间指定,比如删除 data6.txt 文件内容中的第 2、3行:

[root@server1 ~]# sed '2,3d' data6.txt
This is line number 1.
This is line number 4.

在此强调,在默认情况下 sed 并不会修改原始文件,这里被删除的行只是从 sed 的输出中消失了,原始文件没做任何改变

  1. sed ai 脚本命令
    a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行,这里之所以要同时介绍这 2 个脚本命令,因为它们的基本格式完全相同,如下所示:
[address]a(或 i)\新文本内容

示例1: 将一个新行插入到数据流第三行前,执行命令如下:

[root@server1 ~]# sed '3i\
> This is an inserted line.' data6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.

示例2: 再比如说,将一个新行附加到数据流中第三行后,执行命令如下:

[root@server1 ~]# sed '3a\
> This is an appended line.' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.
  1. sed c 替换脚本命令
    c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。该命令的基本格式为:
[address]c\用于替换的新文本

示例1:

[root@server1 ~]# sed '3c\
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.

在这个例子中,sed 编辑器会修改第三行中的文本,其实,下面的写法也可以实现此目的
示例2:

[root@server1 ~]# sed '/number 3/c\
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
  1. sed y 转换脚本命令
    y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:
[address]y/inchars/outchars/

转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符…这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息

eg1:

[root@server1 ~]# cat data6.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@server1 ~]#  sed 'y/123/789/' data6.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.

可以看到,inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相同位置的那个字符

转换命令是一个全局命令,也就是说,它会文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置,再打个比方:

eg2:

[root@server1 ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.

sed 转换了在文本行中匹配到的字符 1 的两个实例,我们无法限定只转换在特定地方出现的字

  1. sed p 打印脚本命令
    p 命令表示搜索符号条件的行,并输出该行的内容,此命令的基本格式为:
[address]p

p 命令常见的用法是打印包含匹配文本模式的行,例如:

[root@server1 ~]# cat data6.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@server1 ~]# sed -n '/number 3/p' data6.txt
This is line number 3.
  1. sed w 脚本命令
    w 命令用来将文本中指定行的内容写入文件中,此命令的基本格式如下:
[address]w filename

这里的 filename 表示文件名,可以使用相对路径或绝对路径,但不管是哪种,运行 sed 命令的用户都必须有文件的写权限

示例1: 将数据流中的前两行打印到一个文本文件中:

[root@server1 ~]# sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@server1 ~]# cat test.txt 
This is line number 1.
This is line number 2.
  1. sed r 脚本命令
    r 命令用于将一个独立文件的数据插入到当前数据流的指定位置,该命令的基本格式为:
[address]r filename

sed 命令会将 filename 文件中的内容插入到 address 指定行的后面

示例1:

[root@server1 ~]# cat data12.txt 
This is an added line.
This is the second added line.
[root@server1 ~]#  sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.
  1. sed q 退出脚本命令
    q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理

示例1:
可以看到,sed 命令在打印输出第 1 行之后,就停止了,是 q 命令造成的

[root@server1 ~]# sed '1q' test.txt
This is line number 1.
[root@server1 ~]# cat test.txt 
This is line number 1.
This is line number 2.
  1. 以数字形式指定行区间
    当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed 会将文本流中的第一行编号为 1,然后继续按顺序为接下来的行分配行号。
    在脚本命令中,指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里举一个 sed 命令作用到指定行号的例子:
[root@server1 ~]# cat data4.txt 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
[root@server1 ~]# sed '2s/dog/cat/' data4.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

可以看到,sed 只修改地址指定的第二行的文本。

发布了94 篇原创文章 · 获赞 1 · 访问量 1769

猜你喜欢

转载自blog.csdn.net/qq_36417677/article/details/104417082