linux sed命令详细讲解
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
sed命令行格式为:
Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入、删除或替换数据中的文本。但本节要讲的 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据。
sed 会根据脚本命令来处理文本文件中的数据,这些命令要么从命令行中输入,要么存储在一个文本文件中,此命令执行数据的顺序如下:
- 每次仅读取一行内容;
- 根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;
- 将执行结果输出。
当一行数据匹配完成后,它会继续读取下一行数据,并重复这个过程,直到将文件中所有数据处理完毕。
sed 命令的基本格式如下:
sed [选项] [脚本命令] 文件名
| 选项 | 含义 |
|---|---|
| -e 脚本命令 | 该选项会将其后跟的脚本命令添加到已有的命令中。 |
| -f 脚本命令文件 | 该选项会将其后文件中的脚本命令添加到已有的命令中。 |
| -n | 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。 |
| -i | 此选项会直接修改源文件,要慎用。 |
成功使用 sed 命令的关键在于掌握各式各样的脚本命令及格式,它能帮你定制编辑文件的规则。
sed脚本命令
sed s替换脚本命令
此命令的基本格式为:
[address]s/需要替换的内容/要替换的新内容/flags
| flags 标记 | 功能 |
|---|---|
| n | 1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记; |
| g | 对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A; |
| p | 会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。 |
| w file | 将缓冲区中的内容写到指定的 file 文件中; |
| & | 用正则表达式匹配的内容进行替换; |
| \n | 匹配第 n 个子串,该子串之前在 pattern 中用 \(\) 指定。 |
| \ | 转义(转义替换部分包含:&、\ 等)。 |
比如,可以指定sed用新文本替换第几处模式匹配的地方
[root@localhost ~]# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
可以看到,使用数字 2 作为标记的结果就是,sed 编辑器只替换每行中第 2 次出现的匹配模式。
如果要用新文件替换所有匹配的字符串,可以使用 g 标记:
[root@localhost ~]# sed 's/test/trial/g' data4.txt This is a trial of the trial script. This is the second trial of the trial script.
-n+p 只输出被替换命令修改过的行
[root@localhost ~]# cat data5.txt This is a test line. This is a different line. [root@localhost ~]# sed -n 's/test/trial/p' data5.txt This is a trial line.
w 标记会将匹配后的结果保存到指定文件中,比如:
[root@localhost ~]# sed 's/test/trial/w test.txt' data5.txt This is a trial line. This is a different line. [root@localhost ~]#cat test.txt This is a trial line.
sed d 删除脚本命令
此命令的基本格式为:
行号+d
如果没有指定特定行,则会删除文件中所有内容
举例如下:
1.通过行号指定,比如删除 data6.txt 文件内容中的第 3 行:
[root@localhost ~]# cat data6.txt This is line number 1. This is line number 2. This is line number 3. This is line number 4. [root@localhost ~]# sed '3d' data6.txt This is line number 1. This is line number 2. This is line number 4.
2.通过特定行区间指定,比如删除 data6.txt 文件内容中的第 2、3行:
[root@localhost ~]# sed '2,3d' data6.txt This is line number 1. This is line number 4.
3.通过特殊的文件结尾字符,比如删除 data6.txt 文件内容中第 3 行开始的所有的内容:
[root@localhost ~]# sed '3,$d' data6.txt This is line number 1. This is line number 2.
在此强调,在默认情况下 sed 并不会修改原始文件,这里被删除的行只是从 sed 的输出中消失了,原始文件没做任何改变。
sed a 和 i 脚本命令——增加脚本命令
a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行
命令基本格式
行号 a/i \新文本内容
举例如下:
1.将一个新行插入到数据流第三行前
[root@localhost ~]# 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@localhost ~]# 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.
3.将一个多行数据添加到数据流中,要插入或附加的文本中的每一行末尾(除最后一行)添加反斜线即可
[root@localhost ~]# sed '1i\ > This is one line of new text.\ > This is another line of new text.' data6.txt This is one line of new text. This is another line of new text. This is line number 1. This is line number 2. This is line number 3. This is line number 4.
sed c 替换脚本命令
s是部分替换,c是整行替换
格式:行号c\用于替换的新文本
[root@localhost ~]# 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 y 转换脚本命令
y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:
行号y/inchars/outchars/
转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。
[root@localhost ~]# sed 'y/123/789/' data8.txt This is line number 7. This is line number 8. This is line number 9. This is line number 4. This is line number 7 again. This is yet another line. This is the last line in the file
浙公网安备 33010602011771号