Linux 命令 -- sed介绍和简单命令
最近处理文件使用sed有点多,占用了一部分时间查询和学习sed的使用,将查询的资料和自己的使用命令整理;温故知新的同时能方便以后使用。
1. 简介
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
1.1 格式及参数
[root@VM_0_6_centos ~]# sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...
1.2 sed 命令常用选项及含义
谨慎使用 -i 命令,在使用-i命令对原文件进行操作前,使用-n 打印查看操作是否正常
| 选项 | 含义 | 
|---|---|
| -e | 脚本命令 该选项会将其后跟的脚本命令添加到已有的命令中。 | 
| -f | 脚本命令文件 该选项会将其后文件中的脚本命令添加到已有的命令中。 | 
| -n | 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。 | 
| -i | 此选项会直接修改源文件,要慎用。 | 
2. sed 脚本命令
2.1 sed s替换命令
基本格式:
[N]s/pattern/replacement/flags
其中,N 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。
- sed s命令flags标记及功能
 
| flags 标记 | 功能 | 
|---|---|
| n | 1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记; | 
| g | 对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A; | 
| p | 会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。 | 
| w file | 将缓冲区中的内容写到指定的 file 文件中; | 
| & | 用正则表达式匹配的内容进行替换; | 
| \n | 匹配第 n 个子串,该子串之前在 pattern 中用 () 指定。 | 
| \ | 转义(转义替换部分包含:&、\ 等)。 | 
- 原始文件
 
This is a test of the test script.
This is the second test of the test script.
- 指定sed 用新字符串替换第几处模式匹配的字符串
 
# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
- 指定sed 用新字符串替换所有的字符串
 
# 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 选项会禁止 sed 输出,p 标记输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行
 
# cat data5.txt
This is a test line.
This is a different line.
# sed -n 's/test/trial/p' data5.txt
This is a trial line.
- w 将匹配后的结果保存到指定文件中
 
# sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
#cat test.txt
This is a trial line.
- 在指定行行首添加字符串,添加#等特殊字符需要\ 转义
 
# cat test.txt
This is a trial line.
This is a different line.
# sed "2s/^/\#&/g" test.txt
This is a trial line.
#This is a different line.
- 在指定行行尾添加字符串
 
# cat test5.txt
This is a trial line.
This is a different line.
[root@VM_0_6_centos test]# sed "2s/$/&\#/g" test5.txt 
This is a trial line.
This is a different line.#
2.2 sed d 删除命令
- 删除指定行中的所有内容
 
# cat data1.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
# sed 'd' data1.txt
#什么也不输出,证明成了空文件
- 删除指定行
 
# cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
# sed '3d' data6.txt
This is line number 1.
This is line number 2.
This is line number 4.
- 删除包含字符串的行
 
# sed "/[1,7,8]/d" test6.txt
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
This is line number 6.
- 删除指定区间
 
# sed '2,3d' data6.txt
This is line number 1.
This is line number 4.
- 删除最后一行
 
# sed '$d' filename
2.3 sed a,i 添加命令
a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行
[N]a(或 i)\新文本内容
- 在指定行前插入新的行
 
# 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.
- 在指定行后插入新的行
 
# sed '3a\
> 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.
- 将一个多行数据添加到数据流中
 
# 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 '3a\
> 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.4 sed c 替换命令
替换指定行的内容,替换成该选项后面的字符串:
[N]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 编辑器会修改第三行中的文本,其实,下面的写法也可以实现此目的:
[root@localhost ~]# 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.
2.5 sed y 转换命令
y 命令是唯一可以处理单个字符的 sed 脚本命令,转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。
[N]y/inchars/outchars/
例如:
# sed 'y/123/456/'  test6.txt 
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
# cat test6.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
转换命令是一个全局命令,也就是说,它会文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置;
# echo "This 1 is a test of 1 try." | sed 'y/123/456/' 
This 4 is a test of 4 try.
2.6 sed n p 打印操作
用 -n 选项和 p 命令配合使用,我们可以禁止输出其他行,只打印包含匹配文本模式的行。
# sed -n '3p' test6.txt 
This is line number 3.
# sed -n '3s/line/test/p' test6.txt 
This is test number 3.
2.7 sed w 写入文件
w 命令用来将文本中指定行的内容写入文件中
[N]w filename
- 将数据流中的前两行打印到一个文本文件
 
# 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@localhost ~]# cat test.txt
This is line number 1.
This is line number 2.
2.8 r 写入文件
将一个独立文件的数据插入到当前数据流的指定位置;将data12.txt 插入到data6.txt的第3行后面
# cat data12.txt
This is an added line.
This is the second added line.
[root@localhost ~]# 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.
                    
                
                
            
        
浙公网安备 33010602011771号